首页 > 代码库 > xtu summer individual 5 E - Burning Bridges
xtu summer individual 5 E - Burning Bridges
Burning Bridges
64-bit integer IO format: %lld Java class name: Main
Ferry Kingdom is a nice little country located on N islands that are connected by M bridges. All bridges are very beautiful and are loved by everyone in the kingdom. Of course, the system of bridges is designed in such a way that one can get from any island to any other one.
But recently the great sorrow has come to the kingdom. Ferry Kingdom was conquered by the armies of the great warrior Jordan and he has decided to burn all the bridges that connected the islands. This was a very cruel decision, but the wizards of Jordan have advised him no to do so, because after that his own armies would not be able to get from one island to another. So Jordan decided to burn as many bridges as possible so that is was still possible for his armies to get from any island to any other one.
Now the poor people of Ferry Kingdom wonder what bridges will be burned. Of course, they cannot learn that, because the list of bridges to be burned is kept in great secret. However, one old man said that you can help them to find the set of bridges that certainly will not be burned.
So they came to you and asked for help. Can you do that?
Input
The input contains multiple test cases. The first line of the input is a single integer T (1 <= T <= 20) which is the number of test cases. T test cases follow, each preceded by a single blank line.
The first line of each case contains N and M - the number of islands and bridges in Ferry Kingdom respectively (2 <= N <= 10 000, 1 <= M <= 100 000). Next M lines contain two different integer numbers each and describe bridges. Note that there can be several bridges between a pair of islands.
Output
On the first line of each case print K - the number of bridges that will certainly not be burned. On the second line print K integers - the numbers of these bridges. Bridges are numbered starting from one, as they are given in the input.
Two consecutive cases should be separated by a single blank line. No blank line should be produced after the last test case.
Sample Input
26 71 22 32 45 41 34 53 610 162 63 76 55 95 41 29 86 42 103 87 91 42 410 51 66 10
Sample Output
23 714
Source
Author
解题:割边、桥。。。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <vector> 6 #include <climits> 7 #include <algorithm> 8 #include <cmath> 9 #define LL long long10 #define INF 0x3f3f3f3f11 using namespace std;12 const int maxn = 10010;13 struct arc{14 int to,num,id;15 arc():num(0){}16 };17 vector<arc>g[maxn];18 int low[maxn],dfn[maxn],ans[maxn*10],id,_index;19 int tot,n,m;20 void addArc(int u,int v){21 bool flag = true;22 int i,j;23 for(i = 0; i < g[u].size(); i++){24 if(v == g[u][i].to){25 flag = false;26 g[u][i].num++;27 for(j = 0; j < g[v].size(); j++){28 if(g[v][j].to == u){29 g[v][j].num++;30 break;31 }32 }33 id++;34 break;35 }36 }37 if(flag){38 arc temp;39 temp.num = 1;40 temp.id = id++;41 temp.to = v;42 g[u].push_back(temp);43 temp.to = u;44 g[v].push_back(temp);45 }46 }47 void tarjan(int u,int fa){48 int v,i,j;49 dfn[u] = low[u] = _index++;50 for(i = 0; i < g[u].size(); i++){51 v = g[u][i].to;52 if(dfn[v] == -1){53 tarjan(v,u);54 low[u] = min(low[u],low[v]);55 }else if(v != fa) low[u] = min(low[u],dfn[v]);56 }57 if(dfn[u] == low[u]){58 v = u;59 u = fa;60 for(i = 0; i < g[u].size(); i++){61 if(v == g[u][i].to){62 if(g[u][i].num == 1) ans[tot++] = g[u][i].id;63 break;64 }65 }66 }67 }68 int main(){69 int t,i,j,u,v;70 scanf("%d",&t);71 while(t--){72 tot = 0;73 id = _index = 1;74 scanf("%d %d",&n,&m);75 for(i = 0; i <= n; i++){76 dfn[i] = low[i] = -1;77 g[i].clear();78 }79 for(i = 0; i < m; i++){80 scanf("%d%d",&u,&v);81 addArc(u,v);82 }83 dfn[1] = low[1] = _index++;84 tarjan(1,1);85 printf("%d\n",tot);86 if(tot){87 sort(ans,ans+tot);88 printf("%d",ans[0]);89 for(i = 1; i < tot; i++)90 printf(" %d",ans[i]);91 puts("");92 }93 if(t) puts("");94 }95 return 0;96 }