地址:http://codeforces.com/contest/369/problem/C
思路:本来是想自己写,但是路是双向的,所以写不动了。看了下大神的代码,好简便。
记录现在答案数并向下深搜,当出现最底层节点时回溯,知道遇见第一个坏的道路时,判断答案数是否有增长。如无增长则说明给节点向下的节点中没有作为修路起点的节点。因此该节点可以添加到答案中。
代码:
#include<iostream>
//#include<cmath>
#include<vector>
#include<cstdio>
#include<cstring>
//#include<algorithm>
using namespace std;
//#define M 11111
vector<int> tree[100010],ans;
void dfs(int st,int x,int t)
{
int i,y,z,l=ans.size(); //在这里保存已得到的ans的个数
for(i=0;i<tree[x].size();i++)
{
y=tree[x][i];
if(y<0) {z=2;y*=-1;}
else z=1;
if(y==st) continue;
dfs(x,y,z);
}
if(t==2&&ans.size()==l) //这里判断条件有两个,一个是路是坏的,一个是ans的个数没有增加
ans.push_back(x);
}
int main()
{
int i,n,x,y,t;
scanf("%d",&n);
for(i=0;i<n-1;i++)
{
scanf("%d%d%d",&x,&y,&t);
tree[x].push_back((3-2*t)*y); //3-2*t大于0表示路没坏,小于0表示路坏了
tree[y].push_back((3-2*t)*x);
}
dfs(0,1,1);
printf("%d\n",ans.size());
for(i=0;i<ans.size();i++) printf("%d ",ans[i]);
return 0;
}