最小树形图裸题,只是须要记录路径

E. Road Repairs
time limit per test

2 seconds

memory limit per test

256 megabytes

input

input.txt

output

output.txt

A country named Berland has n cities. They are numbered with integers from 1 to n.
City with index 1 is the capital of the country. Some pairs of cities have monodirectional roads built between them. However, not all of them
are in good condition. For each road we know whether it needs repairing or not. If a road needs repairing, then it is forbidden to use it. However, the Berland government can repair the road so that it can be used.

Right now Berland is being threatened by the war with the neighbouring state. So the capital officials decided to send a military squad to each city. The squads can move only along the existing roads, as there's no time or money to build new roads. However,
some roads will probably have to be repaired in order to get to some cities.

Of course the country needs much resources to defeat the enemy, so you want to be careful with what you're going to throw the forces on. That's why the Berland government wants to repair the minimum number of roads that is enough for the military troops to
get to any city from the capital, driving along good or repaired roads. Your task is to help the Berland government and to find out, which roads need to be repaired.

Input

The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 105) —
the number of cities and the number of roads in Berland.

Next m lines contain three space-separated integers ai, bi, ci (1 ≤ ai, bi ≤ n, ai ≠ bi, 0 ≤ ci ≤ 1),
describing the road from city ai to
city bi.
If ci equals 0,
than the given road is in a good condition. If ci equals 1,
then it needs to be repaired.

It is guaranteed that there is not more than one road between the cities in each direction.

Output

If even after all roads are repaired, it is still impossible to get to some city from the capital, print  - 1. Otherwise, on the first line
print the minimum number of roads that need to be repaired, and on the second line print the numbers of these roads, separated by single spaces.

The roads are numbered starting from 1 in the order, in which they are given in the input.

If there are multiple sets, consisting of the minimum number of roads to repair to make travelling to any city from the capital possible, print any of them.

If it is possible to reach any city, driving along the roads that already are in a good condition, print 0 in the only output line.

Sample test(s)
input
  1. 3 2
  2. 1 3 0
  3. 3 2 1
output
  1. 1
  2. 2
input
  1. 4 4
  2. 2 3 0
  3. 3 4 0
  4. 4 1 0
  5. 4 2 1
output
  1. -1
input
  1. 4 3
  2. 1 2 0
  3. 1 3 0
  4. 1 4 0
output
  1. 0

  1. /* ***********************************************
  2. Author :CKboss
  3. Created Time :2015年07月07日 星期二 22时48分41秒
  4. File Name :CF240E.cpp
  5. ************************************************ */
  6.  
  7. #include <iostream>
  8. #include <cstdio>
  9. #include <cstring>
  10. #include <algorithm>
  11. #include <string>
  12. #include <cmath>
  13. #include <cstdlib>
  14. #include <vector>
  15. #include <queue>
  16. #include <set>
  17. #include <map>
  18.  
  19. using namespace std;
  20.  
  21. const int INF=0x3f3f3f3f;
  22. const int maxn=2001000;
  23.  
  24. int n,m;
  25.  
  26. struct Edge
  27. {
  28. int u,v,cost,id,ru,rv,rcost;
  29. }edge[maxn];
  30.  
  31. void add_Edge(int id,int u,int v,int c)
  32. {
  33. edge[id].id=id;
  34. edge[id].u=edge[id].ru=u;
  35. edge[id].v=edge[id].rv=v;
  36. edge[id].cost=edge[id].rcost=c;
  37. }
  38.  
  39. int pre[maxn],id[maxn],vis[maxn],in[maxn];
  40.  
  41. //// !!!!
  42. int preid[maxn],useE[maxn];
  43. int eA[maxn],eD[maxn];
  44. int ex;
  45.  
  46. int zhuliu(int root,int n,int m,Edge edge[])
  47. {
  48. int ex=m,res=0;
  49. while(true)
  50. {
  51. for(int i=0;i<n;i++) in[i]=INF;
  52. for(int i=0;i<m;i++)
  53. {
  54. if(edge[i].u!=edge[i].v&&edge[i].cost<in[edge[i].v])
  55. {
  56. pre[edge[i].v]=edge[i].u;
  57. in[edge[i].v]=edge[i].cost;
  58.  
  59. //// !!!!
  60. preid[edge[i].v]=edge[i].id;
  61. }
  62. }
  63. for(int i=0;i<n;i++)
  64. if(i!=root&&in[i]==INF) return -1;
  65. int tn=0;
  66. memset(id,-1,sizeof(id));
  67. memset(vis,-1,sizeof(vis));
  68. in[root]=0;
  69. for(int i=0;i<n;i++)
  70. {
  71. res+=in[i];
  72. int v=i;
  73. //// !!!!
  74. if(i!=root) useE[preid[i]]++;
  75. while(vis[v]!=i&&id[v]==-1&&v!=root)
  76. {
  77. vis[v]=i; v=pre[v];
  78. }
  79. if(v!=root&&id[v]==-1)
  80. {
  81. for(int u=pre[v];u!=v;u=pre[u]) id[u]=tn;
  82. id[v]=tn++;
  83. }
  84. }
  85. if(tn==0) break;
  86. for(int i=0;i<n;i++)
  87. if(id[i]==-1) id[i]=tn++;
  88. for(int i=0;i<m;i++)
  89. {
  90. int v=edge[i].v;
  91. edge[i].u=id[edge[i].u];
  92. edge[i].v=id[edge[i].v];
  93. if(edge[i].u!=edge[i].v)
  94. {
  95. edge[i].cost-=in[v];
  96. //// !!!!
  97. eA[ex]=edge[i].id;
  98. eD[ex]=preid[v];
  99. edge[i].id=ex;
  100. ex++;
  101. }
  102. }
  103. n=tn;
  104. root=id[root];
  105. }
  106.  
  107. //// !!!!
  108. for(int i=ex-1;i>=m;i--)
  109. {
  110. if(useE[i])
  111. {
  112. useE[eA[i]]++; useE[eD[i]]--;
  113. }
  114. }
  115.  
  116. return res;
  117. }
  118.  
  119. int main()
  120. {
  121. freopen("input.txt","r",stdin);
  122. freopen("output.txt","w",stdout);
  123.  
  124. scanf("%d%d",&n,&m);
  125.  
  126. for(int i=0,a,b,c;i<m;i++)
  127. {
  128. scanf("%d%d%d",&a,&b,&c);
  129. a--; b--;
  130. add_Edge(i,a,b,c);
  131. }
  132.  
  133. int lens = zhuliu(0,n,m,edge);
  134.  
  135. if(lens==0||lens==-1) { printf("%d\n",lens); return 0; }
  136.  
  137. printf("%d\n",lens);
  138. for(int i=0;i<m;i++)
  139. {
  140. if(useE[i]&&edge[i].rcost)
  141. printf("%d ",i+1);
  142. }
  143. putchar(10);
  144.  
  145. return 0;
  146. }

Codeforces 240E. Road Repairs 最小树形图+输出路径的更多相关文章

  1. CF240E Road Repairs(最小树形图-记录路径)

    A country named Berland has n cities. They are numbered with integers from 1 to n. City with index 1 ...

  2. hdu 5092 线裁剪(纵向连线最小和+输出路径)

    http://acm.hdu.edu.cn/showproblem.php?pid=5092 给一个m*n的矩阵,找到一个纵向的"线"使得线上的和最小并输出这条线,线能向8个方向延 ...

  3. codeforce 240E 最小树形图+路径记录更新

    最小树形图的路径是在不断建立新图的过程中更新的,因此需要开一个结构体cancle记录那些被更新的边,保存可能会被取消的边和边在旧图中的id 在朱刘算法最后添加了一个从后往前遍历新建边的循环,这可以理解 ...

  4. Codeforces Gym-102219 2019 ICPC Malaysia National E. Optimal Slots(01背包+输出路径)

    题意:给你一个体积为\(T\)的背包,有\(n\)个物品,每个物品的价值和体积都是是\(a_{i}\),求放哪几个物品使得总价值最大,输出它们,并且输出价值的最大值. 题解:其实就是一个01背包输出路 ...

  5. HDU 2121 Ice_cream’s world II 不定根最小树形图

    题目链接: 题目 Ice_cream's world II Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...

  6. HDU 3251 Being a Hero(最小割+输出割边)

    Problem DescriptionYou are the hero who saved your country. As promised, the king will give you some ...

  7. 【BZOJ 2753】 2753: [SCOI2012]滑雪与时间胶囊 (分层最小树形图,MST)

    2753: [SCOI2012]滑雪与时间胶囊 Time Limit: 50 Sec  Memory Limit: 128 MBSubmit: 2457  Solved: 859 Descriptio ...

  8. kuangbin带你飞 生成树专题 : 次小生成树; 最小树形图;生成树计数

    第一个部分 前4题 次小生成树 算法:首先如果生成了最小生成树,那么这些树上的所有的边都进行标记.标记为树边. 接下来进行枚举,枚举任意一条不在MST上的边,如果加入这条边,那么肯定会在这棵树上形成一 ...

  9. HDU 2121——Ice_cream’s world II——————【最小树形图、不定根】

    Ice_cream’s world II Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...

随机推荐

  1. zoj_3657,12年长春站c题,模拟

    #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> us ...

  2. invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause

    Column 'dbo.tbm_vie_View.ViewID' is invalid in the select list because it is not contained in either ...

  3. sql server management studio 快速折叠object explorer中的instance

    https://social.msdn.microsoft.com/Forums/sqlserver/en-US/6e20fa7a-c0a9-496b-89b2-19c6bd996ffc/how-to ...

  4. AvtiveMQ 参考

    推荐学习:https://www.cnblogs.com/zhuxiaojie/p/5564187.html#autoid-2-1-0

  5. Nginx下部署Laravel项目

    Nginx下部署Laravel项目 标签(空格分隔): php Nginx配置文件 listen 80 default_server; #listen [::]:80 default_server i ...

  6. WebView的坑[持续更新]

    返回错误的 innerHeight,如 240(WebView returns bad window.innerHeight) http://stackoverflow.com/questions/1 ...

  7. 前端验证银行卡(Luhn校验算法)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. [Bug]Python3.x SyntaxError: 'ascii' codec can't decode byte 0xe4 in position

    安装arch后就没再用python了 昨天管服务器的大佬在跑贝叶斯分类器的时候发现正确率有问题 我赶紧去做优化,然后就有这样的报错 Python 3.6.4 (default, Jan 5 2018, ...

  9. linux指令快速复制粘贴[龟速更新中]

    由于有经常碰到要输入linux指令,但是却忘记了的情况.在家里我把常用的命令放到Xshell的快速命令集,但是在很多情况下不在家,可能用的他人电脑,以及在非Win环境下使用ssh时没有xshell使用 ...

  10. whatis---查询一个命令执行什么功能

    whatis命令是用于查询一个命令执行什么功能,并将查询结果打印到终端上. whatis命令在用catman -w命令创建的数据库中查找command参数指定的命令.系统调用.库函数或特殊文件名.wh ...