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

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
3 2
1 3 0
3 2 1
output
1
2
input
4 4
2 3 0
3 4 0
4 1 0
4 2 1
output
-1
input
4 3
1 2 0
1 3 0
1 4 0
output
0

/* ***********************************************
Author :CKboss
Created Time :2015年07月07日 星期二 22时48分41秒
File Name :CF240E.cpp
************************************************ */ #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map> using namespace std; const int INF=0x3f3f3f3f;
const int maxn=2001000; int n,m; struct Edge
{
int u,v,cost,id,ru,rv,rcost;
}edge[maxn]; void add_Edge(int id,int u,int v,int c)
{
edge[id].id=id;
edge[id].u=edge[id].ru=u;
edge[id].v=edge[id].rv=v;
edge[id].cost=edge[id].rcost=c;
} int pre[maxn],id[maxn],vis[maxn],in[maxn]; //// !!!!
int preid[maxn],useE[maxn];
int eA[maxn],eD[maxn];
int ex; int zhuliu(int root,int n,int m,Edge edge[])
{
int ex=m,res=0;
while(true)
{
for(int i=0;i<n;i++) in[i]=INF;
for(int i=0;i<m;i++)
{
if(edge[i].u!=edge[i].v&&edge[i].cost<in[edge[i].v])
{
pre[edge[i].v]=edge[i].u;
in[edge[i].v]=edge[i].cost; //// !!!!
preid[edge[i].v]=edge[i].id;
}
}
for(int i=0;i<n;i++)
if(i!=root&&in[i]==INF) return -1;
int tn=0;
memset(id,-1,sizeof(id));
memset(vis,-1,sizeof(vis));
in[root]=0;
for(int i=0;i<n;i++)
{
res+=in[i];
int v=i;
//// !!!!
if(i!=root) useE[preid[i]]++;
while(vis[v]!=i&&id[v]==-1&&v!=root)
{
vis[v]=i; v=pre[v];
}
if(v!=root&&id[v]==-1)
{
for(int u=pre[v];u!=v;u=pre[u]) id[u]=tn;
id[v]=tn++;
}
}
if(tn==0) break;
for(int i=0;i<n;i++)
if(id[i]==-1) id[i]=tn++;
for(int i=0;i<m;i++)
{
int v=edge[i].v;
edge[i].u=id[edge[i].u];
edge[i].v=id[edge[i].v];
if(edge[i].u!=edge[i].v)
{
edge[i].cost-=in[v];
//// !!!!
eA[ex]=edge[i].id;
eD[ex]=preid[v];
edge[i].id=ex;
ex++;
}
}
n=tn;
root=id[root];
} //// !!!!
for(int i=ex-1;i>=m;i--)
{
if(useE[i])
{
useE[eA[i]]++; useE[eD[i]]--;
}
} return res;
} int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout); scanf("%d%d",&n,&m); for(int i=0,a,b,c;i<m;i++)
{
scanf("%d%d%d",&a,&b,&c);
a--; b--;
add_Edge(i,a,b,c);
} int lens = zhuliu(0,n,m,edge); if(lens==0||lens==-1) { printf("%d\n",lens); return 0; } printf("%d\n",lens);
for(int i=0;i<m;i++)
{
if(useE[i]&&edge[i].rcost)
printf("%d ",i+1);
}
putchar(10); return 0;
}

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. js绑定事件方法:addEventListener与attachEvent的不同浏览器的兼容性写法

    js的事件绑定方法中,ie仅仅支持attachEvent,而FF和Chrome仅仅支持addEventListener,所以就必须为这两个方法做兼容处理,原理是先推断attachEvent仅仅否为真( ...

  2. 基于opencv的手写数字字符识别

    摘要 本程序主要参照论文,<基于OpenCV的脱机手写字符识别技术>实现了,对于手写阿拉伯数字的识别工作.识别工作分为三大步骤:预处理,特征提取,分类识别.预处理过程主要找到图像的ROI部 ...

  3. Saying Good-bye to Cambridge Again

    Saying Good-bye to Cambridge Again Very quietly I take my leave,      As quietly as I came here;     ...

  4. Svn中的tag标签的用法和意义

    使用场景: 假如你的项目的某个版本已经完成测试开发.测试并已经上线,接下来街道新的需求,新项目开发需要修改多个文件的代码,当需求已经开发一段时间的时候,突然接到用户和测试人员的反馈,项目中某个重大的b ...

  5. 《剑指offer》调整数组顺序使奇数位于偶数前面

    一.题目描述 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变. 二.输入描述 ...

  6. python实现决策树C4.5算法(在ID3基础上改进)

    一.概论 C4.5主要是在ID3的基础上改进,ID3选择(属性)树节点是选择信息增益值最大的属性作为节点.而C4.5引入了新概念"信息增益率",C4.5是选择信息增益率最大的属性作 ...

  7. Matlab函数编译成dll供c调用

    一 编译dll 在Command Window窗口中输入mbuild -setup,然后会出现语句,是否安装编译器,选择n,因为机子上已经安装了C/C++/C#的编译器,选择VS2010.

  8. 初入AngularJS

    AngularJS是一款优秀的前端JS框架,已经被用于Google的多款产品当中.AngularJS有着诸多特性,最为核心的是:MVVM.模块化.自动化双向数据绑定.语义化标签.依赖注入等等. Ang ...

  9. ng-show ng-hide ng-if的区别

    用途 ng-show ng-hide ng-if三个都可以用来控制页面DOM元素的显示与隐藏. ng-hide条件为true时,隐藏所在元素,false时显示所在元素. ng-show相反,条件为tr ...

  10. vSphere VCSA5.5加入AD域环境问题记录

    vSphere VCSA5.5加入AD域环境问题记录 实验目的: 搭建一套vSphere VCSA5.5,并加入新搭建的AD域,并使用一个域用户登录VC,赋予对VC的只读权限. 实验环境: 使用VMW ...