POJ 3694 tarjan 桥+lca

Network
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 7298 Accepted: 2651

Description

A network administrator manages a large network. The network consists of N computers and M links between pairs of computers. Any pair of computers are connected directly or indirectly by successive links, so data can be transformed between any two computers. The administrator finds that some links are vital to the network, because failure of any one of them can cause that data can't be transformed between some computers. He call such a link a bridge. He is planning to add some new links one by one to eliminate all bridges.

You are to help the administrator by reporting the number of bridges in the network after each new link is added.

Input

The input consists of multiple test cases. Each test case starts with a line containing two integers N(1 ≤ N ≤ 100,000) and M(N - 1 ≤ M ≤ 200,000).
Each of the following M lines contains two integers A and B ( 1≤ A ≠ B ≤ N), which indicates a link between computer A and B. Computers are numbered from 1 to N. It is guaranteed that any two computers are connected in the initial network.
The next line contains a single integer Q ( 1 ≤ Q ≤ 1,000), which is the number of new links the administrator plans to add to the network one by one.
The i-th line of the following Q lines contains two integer A and B (1 ≤ A ≠ B ≤ N), which is the i-th added new link connecting computer A and B.

The last test case is followed by a line containing two zeros.

Output

For each test case, print a line containing the test case number( beginning with 1) and Q lines, the i-th of which contains a integer indicating the number of bridges in the network after the first i new links are added. Print a blank line after the output for each test case.

Sample Input

3 2
1 2
2 3
2
1 2
1 3
4 4
1 2
2 1
2 3
1 4
2
1 2
3 4
0 0

Sample Output

Case 1:
1
0

Case 2:
2
0

Source

 
题目意思:
n个节点,m条无向边的图。然后q个操作,每个操作连接u、v点,然后输出连接后图中剩余桥的个数。
 
思路:
先tarjan标记一下桥,然后lca把路径上所有桥去掉标记。
 
代码:
  1 #include <cstdio>
  2 #include <cstring>
  3 #include <algorithm>
  4 #include <iostream>
  5 #include <vector>
  6 #include <queue>
  7 #include <cmath>
  8 #include <set>
  9 #include <map>
 10 #include <stack>
 11 using namespace std;
 12 
 13 #define N 100005
 14 
 15 int n, m;
 16 int dfn[N], low[N], Time;
 17 int dep[N];
 18 bool brg[N];
 19 int father[N];
 20 int brg_num;
 21 
 22 struct Edge{
 23     int u, v, next;
 24 }e[400005];
 25 
 26 int cnt;
 27 int head[N];
 28 
 29 void setEdge(int u,int v){
 30     e[cnt].u=u;e[cnt].v=v;
 31     e[cnt].next=head[u];head[u]=cnt++;
 32 }
 33 
 34 void tarjan(int u,int fa){
 35     int i, v;
 36     dfn[u]=low[u]=Time++;
 37     for(i=head[u];i!=-1;i=e[i].next){
 38         v=e[i].v;
 39         if(v==fa) continue;
 40         if(!dfn[v]){
 41             dep[v]=dep[u]+1;
 42             father[v]=u;
 43             tarjan(v,u);
 44             if(low[v]>dfn[u]){
 45                 brg_num++;
 46                 brg[v]=true;
 47             }
 48             low[u]=min(low[u],low[v]);
 49         }
 50         else low[u]=min(dfn[v],low[u]);
 51     }
 52 }
 53 
 54 void lca(int u,int v){
 55     while(dep[u]>dep[v]){
 56         if(brg[u]) {
 57             brg[u]=false,brg_num--;
 58         }
 59         u=father[u];
 60     }
 61     while(dep[v]>dep[u]){
 62         if(brg[v]) {
 63             brg[v]=false,brg_num--;
 64         }
 65         v=father[v];
 66     }
 67     while(u!=v){
 68         if(brg[u]) {
 69             brg[u]=false,brg_num--;
 70         }
 71         u=father[u];
 72         if(brg[v]) {
 73             brg[v]=false,brg_num--;
 74         }
 75         v=father[v];
 76     }
 77 }
 78 
 79 main()
 80 {
 81     int i, j, k;
 82     int u, v;
 83     int kase=1;
 84     while(scanf("%d %d",&n,&m)==2){
 85         if(!n&&!m) break;
 86         memset(head,-1,sizeof(head));
 87         cnt=0;
 88         for(i=0;i<m;i++){
 89             scanf("%d %d",&u,&v);
 90             setEdge(u,v);
 91             setEdge(v,u);
 92         }
 93         Time=1;
 94         memset(dfn,0,sizeof(dfn));
 95         memset(brg,false,sizeof(brg));
 96         brg_num=0;
 97         for(i=1;i<=n;i++){
 98             if(!dfn[i]){
 99                 dep[i]=0;
100                 tarjan(i,i);
101             }
102         }
103         printf("Case %d:\n",kase++);
104         int q;
105         scanf("%d",&q);
106         for(i=0;i<q;i++){
107             scanf("%d %d",&u,&v);
108             lca(u,v);
109             printf("%d\n",brg_num);
110         }
111         cout<<endl;
112     }
113 }

 

转载于:https://www.cnblogs.com/qq1012662902/p/4646670.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
大学生参加学科竞赛有着诸多好处,不仅有助于个人综合素质的提升,还能为未来职业发展奠定良好基础。以下是一些分析: 首先,学科竞赛是提高专业知识和技能水平的有效途径。通过参与竞赛,学生不仅能够深入学习相关专业知识,还能够接触到最新的科研成果和技术发展趋势。这有助于拓展学生的学科视野,使其对专业领域有更深刻的理解。在竞赛过程中,学生通常需要解决实际问题,这锻炼了他们独立思考和解决问题的能力。 其次,学科竞赛培养了学生的团队合作精神。许多竞赛项目需要团队协作来完成,这促使学生学会有效地与他人合作、协调分工。在团队合作中,学生们能够学到如何有效沟通、共同制定目标和分工合作,这对于日后进入职场具有重要意义。 此外,学科竞赛是提高学生综合能力的一种途径。竞赛项目通常会涉及到理论知识、实际操作和创新思维等多个方面,要求参赛者具备全面的素质。在竞赛过程中,学生不仅需要展现自己的专业知识,还需要具备创新意识和解决问题的能力。这种全面的综合能力培养对于未来从事各类职业都具有积极作用。 此外,学科竞赛可以为学生提供展示自我、树立信心的机会。通过比赛的舞台,学生有机会展现自己在专业领域的优势,得到他人的认可和赞誉。这对于培养学生的自信心和自我价值感非常重要,有助于他们更加积极主动地投入学习和未来的职业生涯。 最后,学科竞赛对于个人职业发展具有积极的助推作用。在竞赛中脱颖而出的学生通常能够引起企业、研究机构等用人单位的关注。获得竞赛奖项不仅可以作为个人履历的亮点,还可以为进入理想的工作岗位提供有力的支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值