CodeForces - 937D Sleepy Game [dfs+DP]
2018-03-05 21:53
302 查看
题意:给一个有向图,问能不能奇数步走到出度为0的点。
题解:dp[i][j]表示走到当前节点为奇数或者偶数是否存在,pre[i]记录路径。
AC代码:
#include<stdio.h>
#include<vector>
#include<string.h>
using namespace std;
vector<int>vt[100005];
int chu[100005];
int dp[100005][2];
int pre[100005][2];
int mark[100005],ok;
int ans[1000005];
void dfs(int u,int step)
{
for(int i=0;i<vt[u].size();i++)
{
int to=vt[u][i];
if(mark[to])ok=1;
if(dp[to][step^1])continue;
pre[to][step^1]=u;
dp[to][step^1]=1;
mark[to]=1;
dfs(to,step^1);
mark[to]=0;
}
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d",&chu[i]);
for(int j=0;j<chu[i];j++)
{
int v;
scanf("%d",&v);
vt[i].push_back(v);
}
}
int s;
scanf("%d",&s);
dp[s][0]=1;
mark[s]=1;
dfs(s,0);
for(int i=1;i<=n;i++)
if(!chu[i])
{
if(dp[i][1])
{
int tot=0;
printf("Win\n");
int now=i,f=1;
while(now!=s||f!=0)
{
ans[tot++]=now;
now=pre[now][f];
f^=1;
}
printf("%d",s);
for(int i=tot-1;i>=0;i--)
printf(" %d",ans[i]);
printf("\n");
return 0;
}
}
if(ok)printf("Draw\n");
else printf("Lose\n");
}
题解:dp[i][j]表示走到当前节点为奇数或者偶数是否存在,pre[i]记录路径。
AC代码:
#include<stdio.h>
#include<vector>
#include<string.h>
using namespace std;
vector<int>vt[100005];
int chu[100005];
int dp[100005][2];
int pre[100005][2];
int mark[100005],ok;
int ans[1000005];
void dfs(int u,int step)
{
for(int i=0;i<vt[u].size();i++)
{
int to=vt[u][i];
if(mark[to])ok=1;
if(dp[to][step^1])continue;
pre[to][step^1]=u;
dp[to][step^1]=1;
mark[to]=1;
dfs(to,step^1);
mark[to]=0;
}
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d",&chu[i]);
for(int j=0;j<chu[i];j++)
{
int v;
scanf("%d",&v);
vt[i].push_back(v);
}
}
int s;
scanf("%d",&s);
dp[s][0]=1;
mark[s]=1;
dfs(s,0);
for(int i=1;i<=n;i++)
if(!chu[i])
{
if(dp[i][1])
{
int tot=0;
printf("Win\n");
int now=i,f=1;
while(now!=s||f!=0)
{
ans[tot++]=now;
now=pre[now][f];
f^=1;
}
printf("%d",s);
for(int i=tot-1;i>=0;i--)
printf(" %d",ans[i]);
printf("\n");
return 0;
}
}
if(ok)printf("Draw\n");
else printf("Lose\n");
}
相关文章推荐
- codeforces 280c 概率dp + dfs
- CodeForces 148D Bag of mice 概率DP , DFS
- Codeforces 258B. Little Elephant and Elections【数位DP,DFS】
- Codeforces 132C.Logo Turtle【DP,dfs】
- CodeForces - 937D Sleepy Game (乘法逆元)
- Codeforces 337 D Book of Evil(树形dp,两遍dfs)
- Codeforces 520B (bfs,dp,dfs,greedy)
- [Codeforces 258B & 259 D]Little Elephant and Elections 数位dp+dfs
- Codeforces-936B:Sleepy Game(DP)
- Codeforces 937D - Sleepy Game 【博弈+判环】
- CodeForces 149D 括号染色问题 dp+dfs好题
- codeforces 132C Logo Turtle dp DFS 搜索答案
- codeforces 55D - Beautiful numbers 数位DP+DFS
- codeforces--55D--Beautiful numbers(数位dp,dfs+记忆化)
- codeforces 258B Little Elephant and Elections 数位DP+DFS
- Codeforces 509F Progress Monitoring 给定dfs序求树的同构数 区间dp
- Codeforces 467D. Fedor and Essay (Graphs,dfs,dp,hashing,strings,图论综合型好题)
- CodeForces 258 B.Little Elephant and Elections(数位DP+dfs)
- CodeForces 149D Coloring Brackets(区间DP+dfs)
- CodeForces - 149D Coloring Brackets 详细题解(递归区间DP+dfs染色,好题)