CF240E Road Repairs(最小樹形圖-記錄路徑)

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.

Examples

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

題解:
題目意思是:給你有向路徑,0表示可以走,1表示這條路需要修復才能走,問你要讓1號點能走到所有點的最小花費是多少。
思路:最小樹形圖就是最小代價,輸出邊的話,就是在zhuliu算法中的縮環的過程中記錄需要增加的邊和要刪除的邊,最後倒着處理一遍,剩下的邊就是最小樹形圖上的邊。
我們字需要輸出邊的類型爲1的邊即可。

參考代碼:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pii pair<int,int>
#define pil pair<int,ll>
#define mkp make_pair
const int INF=0x3f3f3f3f;
const int maxn=1e6+10;
struct Edge{
    int x,y,w;
    int id,real;
} edge[maxn];
int vis[maxn],id[maxn],in[maxn],pre[maxn];
int lastEdge[maxn],used[maxn];
int addEdge[maxn],delEdge[maxn];
int zhuliu(int root,int n,int m)
{
    int res=0,edgeNum=m;
    while(true)
    {
        for(int i=1;i<=n;++i) in[i]=INF;
        for(int i=1;i<=m;++i)//找到每個點的最小入邊及其編號 
        {
            int x=edge[i].x,y=edge[i].y;
            if(edge[i].w<in[y] && x!=y)
            {
                pre[y]=x;
                in[y]=edge[i].w;
                lastEdge[y]=edge[i].id;
            }
        }
        for(int i=1;i<=n;++i)//判斷是否可以形成最小樹形圖 
        {
            if(i==root) continue;
            if(in[i]==INF) return -1;    
        }
        int cnt=0; in[root]=0;
        memset(id,-1,sizeof id);
        memset(vis,-1,sizeof vis);
        for(int i=1;i<=n;++i)
        {
            res+=in[i];
            if(i!=root) used[lastEdge[i]]++;
            int y=i;
            while(vis[y]!=i&&id[y]==-1&&y!=root)
            {
                vis[y]=i;
                y=pre[y];
            }
            if(y!=root && id[y]==-1)
            {
                cnt++;
                for(int x=pre[y];x!=y;x=pre[x]) id[x]=cnt;
                id[y]=cnt;    
            }
        }
        
        if(cnt==0) break;
        for(int i=1;i<=n;++i)//獨立點 
            if(id[i]==-1) id[i]=++cnt;
        
        for(int i=1;i<=m;++i)
        {
            int x=edge[i].x,y=edge[i].y;
            edge[i].x=id[x];edge[i].y=id[y];
            if(id[x]!=id[y])    
            {
                edge[i].w-=in[y];
                delEdge[++edgeNum]=lastEdge[y];
                addEdge[edgeNum]=edge[i].id;
                edge[i].id=edgeNum;    
            }
        }
        n=cnt;
        root=id[root];
    }
    
    for(int i=edgeNum;i>m;--i)
    {
        if(used[i])
        {
            --used[delEdge[i]];
            ++used[addEdge[i]];
        }
    }
    return res;
}

int main()
{
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;++i)
    {
        scanf("%d%d%d",&edge[i].x,&edge[i].y,&edge[i].w);
        edge[i].id=i;
        edge[i].real=edge[i].w;    
    }
    
    int res=zhuliu(1,n,m);
    if(res==-1||res==0) printf("%d\n",res);
    else
    {
        printf("%d\n",res);
        for(int i=1;i<=m;++i)
        {
            if(used[i]&&edge[i].real)
                printf("%d ",i);
        }
        puts("");
    }
    
    return 0;    
}
View Code

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章