codeforces 216 DIV C. Valera and Elections
来源:互联网 发布:淘宝网帮助中心 编辑:程序博客网 时间:2024/04/29 14:25
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
这题用自己常用的map【】【】肯定不行啊,因为有10^5个点,10^5条边,其实以前月神就问过我碰到这样的情况怎么办,当时我不知道的说。他是指针控,这题他是用链表记录的,我和他不一样,我看我们数据结构书上说了稀疏矩阵的三元组表存储,所以这道我就用上了。
这题用dfs就行了,不断往下早,直到遍历完所有的点就行了,具体怎么做,还是看代码吧,我说不清楚。(自己的渣渣的表达能力实在是我自己都受不了自己)
代码:
#include<stdio.h>#include<math.h>#include<string.h>#include<algorithm>#include<limits.h>using namespace std;struct node{int be;int end;int v;}list[211111]; //以三元组表的形式记录图 int hash[211111];//记录以第i点为起点的边的数目 int ans[211111];//记录改点是否需要输出 int sum[211111];//以前i个点为起点的边的数目,方便查找 int vis[211111];// 记录第i个点前面以vis[i]为起点道路的需要修理 int hehe[211111];// 判断该点是否已经遍历了,如果已经遍历过,则不需要再遍历(因为图为n点,n-1条边) int cmp(node x,node y){return x.be < y.be;}void dfs(int t){if(hash[t]==0)return;for(int i= 1,j= sum[t-1]+1; i<= hash[t]; i++,j++){if(hehe[list[j].end])continue;hehe[list[j].end]= hehe[list[j].be] +1;if(list[j].v==1){ans[list[j].end]= 0;vis[list[j].end]= vis[list[j].be];dfs(list[j].end);// 如果该边不需要修理,则 记录之前需要修理的边}else{ans[list[j].end]= 1;ans[list[j].be]= 0;ans[vis[list[j].be]]= 0;vis[list[j].be]= 0;vis[list[j].end]= list[j].end;dfs(list[j].end);// 如果该边需要修理,则list[j].end之前的边都会随之修理,// 所以 ans[vis[list[j].be]]= 0; 代表将之前需要的边删除// 例如 4个点(编号1,2,3,4),点1和点2之间的边需要修理,23之间不需要,34之间需要// 遍历第一条边的时候,ans【2】= 1,vis【2】= 2;// 遍历第二条边的时候,vis【3】= vis【2】= 2; 表示前三个点中,点2之前有边需要修理, // 那么遍历到第三条边的时候,ans【2】 应该变为0,//同时vis【4】= 4,表示点4之前有边需要修理,点2之前没有边需要修理 }}}int main(){int n;while(scanf("%d",&n)!=EOF){memset(ans,0,sizeof(ans));memset(vis,0,sizeof(vis));memset(hash,0,sizeof(hash));memset(sum,0,sizeof(sum));memset(hehe,0,sizeof(hehe));for(int i= 1; i< n; i++){scanf("%d%d%d",&list[i].be,&list[i].end,&list[i].v);hash[list[i].be]++;hash[list[i].end]++;list[n+i-1].be= list[i].end;list[n+i-1].end= list[i].be;list[n+i-1].v= list[i].v;//双向图,每条边记录两次 }sort(list+1,list+2*n-1,cmp);// 排序方便搜索的时候找到起点和终点sum[0]= 0;for(int i=1; i<= n; i++)sum[i]=sum[i-1]+ hash[i]; //记录每个点的起点hehe[1]= 1;dfs(1);int anssum= 0;for(int i=1; i<= n; i++)if(ans[i])anssum++;printf("%d\n",anssum); int flag= 0;for(int i= 1; i<= n; i++)if(ans[i]){if(flag==0){printf("%d",i);flag= 1;}elseprintf(" %d",i);}if(anssum)printf("\n");}}
- codeforces 216 DIV C. Valera and Elections
- Codeforces Round #216 (Div. 2) C. Valera and Elections
- Codeforces Round #216 (Div. 2)---C. Valera and Elections
- Codeforces Round #216 (Div. 2) C. Valera and Elections (树形dp)
- Codeforces Round #216 (Div. 2) C - Valera and Elections(图论和dfs)
- Codeforces Round #216 (diy.2) C. Valera and Elections
- Codeforces Round #216 C Valera and Elections ( DFS )
- CodeForces 369C. Valera and Elections
- codeforces 369C Valera and Elections DFS
- codeforces 369C Valera and Elections
- Codeforces 369C Valera And Elections 树形DP
- Codeforces 369C Valera and Elections【思维+Dfs】
- codeforce C. Valera and Elections (DFS)
- C. Valera and Elections----树形dp
- Codeforces #252 (Div. 2) C. Valera and Tubes
- Codeforces Round #252 (Div. 2) C. Valera and Tubes
- Codeforces Round #252 (Div. 2) 441C. Valera and Tubes
- Codeforces Round #216 (Div. 2) B. Valera and Contest
- Android上dip、dp、px、sp
- 深入研究B树索引(五)
- videoview 有声音没画面的可能原因
- 派生类的构造函数
- linux按照指定关键字杀死进程
- codeforces 216 DIV C. Valera and Elections
- linuxs查看进程运行多久时间
- QT编程经验
- 新建cocos2d-x 项目第一次导入eclipse时,Cocos2dxActivity报错
- linux学习资源
- matlab图像处理
- 酷音铃声获2013年中国第二届移动互联网“拳头奖”最佳娱乐应用奖
- windows 下开发linux c++ 环境搭建
- php合成或者创建gif动画