codeforce C. Valera and Elections (DFS)
来源:互联网 发布:新西兰导航软件mate 编辑:程序博客网 时间:2024/05/03 05:48
给你几组数代表那两个城镇有路连接和路的好坏。。
要找到最少的从城镇1到x的x集合能把所有的坏的路都找到并修好。。
The city Valera lives in is going to hold elections to the city Parliament.
The city has n districts and n - 1 bidirectional roads. We know that from any district there is a path along the roads to any other district. Let's enumerate all districts in some way by integers from 1 to n, inclusive. Furthermore, for each road the residents decided if it is the problem road or not. A problem road is a road that needs to be repaired.
There are n candidates running the elections. Let's enumerate all candidates in some way by integers from 1 to n, inclusive. If the candidate number i will be elected in the city Parliament, he will perform exactly one promise — to repair all problem roads on the way from the i-th district to the district 1, where the city Parliament is located.
Help Valera and determine the subset of candidates such that if all candidates from the subset will be elected to the city Parliament, all problem roads in the city will be repaired. If there are several such subsets, you should choose the subset consisting of the minimum number of candidates.
The first line contains a single integer n (2 ≤ n ≤ 105) — the number of districts in the city.
Then n - 1 lines follow. Each line contains the description of a city road as three positive integers xi, yi, ti (1 ≤ xi, yi ≤ n, 1 ≤ ti ≤ 2) — the districts connected by the i-th bidirectional road and the road type. If ti equals to one, then the i-th road isn't the problem road; if tiequals to two, then the i-th road is the problem road.
It's guaranteed that the graph structure of the city is a tree.
In the first line print a single non-negative number k — the minimum size of the required subset of candidates. Then on the second line print k space-separated integers a1, a2, ... ak — the numbers of the candidates that form the required subset. If there are multiple solutions, you are allowed to print any of them.
51 2 22 3 23 4 24 5 2
15
51 2 12 3 22 4 14 5 1
13
51 2 21 3 21 4 21 5 2
45 4 3 2
#include <stdlib.h>
#include <string.h>
#define MAX 101000
struct snode{
int to,w,next;
}Tree[MAX<<2];
int visit[MAX],pre[MAX],sum[MAX],Index,Answer[MAX],Ansn;
void ADD_edge(int a,int b,int c)
{
Tree[Index].w=c;
Tree[Index].to=b;
Tree[Index].next=pre[a];
pre[a]=Index++;
}
int DFS(int star,int w)
{
visit[star]=1;
int i,v,pd=0,kk=0;
for(i=pre[star];i!=-1;i=Tree[i].next)
{
if(visit[Tree[i].to])
continue;
sum[star]+=DFS(Tree[i].to,Tree[i].w);
}
if(w==2||sum[star]>=1)//这条路坏的||这路的子节点修好了
{
if(sum[star]==0)
Answer[Ansn++]=star;
return 1;
}
else
return 0;
}
int main()
{
int n,i,j,a,b,c,ans;
while(scanf("%d",&n)!=EOF)
{
Index=ans=Ansn=0;
memset(pre,-1,sizeof(pre));
memset(sum,0,sizeof(sum));
memset(visit,0,sizeof(visit));
for(i=1;i<n;i++)
{
scanf("%d %d %d",&a,&b,&c);
ADD_edge(a,b,c);
ADD_edge(b,a,c);//双向
}
visit[1]=1;
ans+=DFS(1,0);//从节点1开始搜
printf("%d\n",Ansn);
for(int i=0;i<Ansn;i++)
{
printf("%d ",Answer[i]);
// if(i!=Ansn)
//printf(" ");
}
puts("");//换行作用
}
//printf("%d\n",MAX<<2);
}
- codeforce C. Valera and Elections (DFS)
- codeforces 369C Valera and Elections DFS
- Codeforces Round #216 C Valera and Elections ( DFS )
- Codeforces 369C Valera and Elections【思维+Dfs】
- CodeForces 369C. Valera and Elections
- codeforces 216 DIV C. Valera and Elections
- codeforces 369C Valera and Elections
- C. Valera and Elections----树形dp
- Codeforces Round #216 (Div. 2) C - Valera and Elections(图论和dfs)
- Codeforces Round #216 (Div. 2) C. Valera and Elections
- Codeforces Round #216 (diy.2) C. Valera and Elections
- Codeforces Round #216 (Div. 2)---C. Valera and Elections
- Codeforces 369C Valera And Elections 树形DP
- codeforce Valera and Plates
- codeforce Valera and Contest
- Codeforces Round #216 (Div. 2) C. Valera and Elections (树形dp)
- C - Valera and Contest题解
- *【codeforce】782C 【dfs】
- Spring中配置数据源
- 第158天
- Java 期望为线性时间的选择算法
- 小算法大世界之冒泡排序VS选择排序
- 037:hibernate一对一双向外键关联(Annotation)
- codeforce C. Valera and Elections (DFS)
- oracle分区表彻底删除的办法
- HDU_1846 Brave Game
- MFC获取文件大小方法
- Hbase 数据导出到文件中
- poj1535(最短路)
- 八数码问题——HDU 1043
- 两个activity相互跳转退不出去
- jboss使用指南