LightOJ 1063 Ant Hills

本文探讨了如何通过分析蚂蚁帝国的蚁丘网络结构,找出可以击毁以阻止至少两个其他蚁丘通信的关键蚁丘,进而停止即将到来的蚂蚁战争。通过实施一种高效的算法,本文揭示了选择正确目标的重要性。

Ant Hills

Time Limit: 2000ms
Memory Limit: 32768KB
This problem will be judged on  LightOJ. Original ID: 1063
64-bit integer IO format: %lld      Java class name: Main

After many years of peace, an ant-war has broken out.

In the days leading up to the outbreak of war, the ant government devoted a great deal of resources toward gathering intelligence on ant hills. It discovered the following:

  1. The ant empire has a large network of ant-hills connected by bidirectional tracks.
  2. It is possible to send a message from any ant hill to any other ant hill.

Now you want to stop the war. Since they sometimes attack your house and disturb you quite a lot. So, you have made a plan. You have a gun which can destroy exactly one ant-hill. So, you want to hit an ant hill if it can stop at least two other ant hills passing messages between them. Now you want the total number of ant hills you may choose to fire.

 

Input

Input starts with an integer T (≤ 20), denoting the number of test cases.

Each test case contains a blank line and two integers n (1 ≤ n ≤ 10000), m (1 ≤ m ≤ 20000)n denotes the number of ant hills and m denotes the number of bi-directional tracks. Each of the next m lines will contain two different integers a b (1 ≤ a, b ≤ n) denoting that there is a track between a and b.

 

Output

For each case, print the case number and the total number of ant hills you may choose to fire.

Sample Input

2

 

5 4

2 1

1 3

5 4

4 1

 

3 3

1 2

2 3

1 3

Sample Output

Case 1: 2

Case 2: 0

 

Source

Problem Setter: Jane Alam Jan
 
解题:求割点数
 
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 10010;
 4 struct arc {
 5     int to,next;
 6     arc(int x = 0,int y = -1) {
 7         to = x;
 8         next = y;
 9     }
10 } e[100000];
11 int head[maxn],dfn[maxn],low[maxn];
12 int tot,idx,ans,n,m;
13 bool vis[maxn];
14 void add(int u,int v) {
15     e[tot] = arc(v,head[u]);
16     head[u] = tot++;
17     e[tot] = arc(u,head[v]);
18     head[v] = tot++;
19 }
20 void tarjan(int u,int fa) {
21     dfn[u] = low[u] = ++idx;
22     int son = 0;
23     for(int i = head[u]; ~i; i = e[i].next) {
24         if(!dfn[e[i].to]) {
25             tarjan(e[i].to,u);
26             low[u] = min(low[u],low[e[i].to]);
27             son++;
28             if(!vis[u]&&(fa == -1 && son > 1 || fa != -1 && low[e[i].to] >= dfn[u])) {
29                 vis[u] = true;
30                 ans++;
31             }
32         } else low[u] = min(low[u],dfn[e[i].to]);
33     }
34 }
35 int main() {
36     int T,u,v,cs = 1;
37     scanf("%d",&T);
38     while(T--) {
39         scanf("%d %d",&n,&m);
40         memset(head,-1,sizeof(head));
41         memset(dfn,0,sizeof(dfn));
42         memset(low,0,sizeof(low));
43         memset(vis,false,sizeof(vis));
44         ans = tot = 0;
45         for(int i = 0; i < m; ++i) {
46             scanf("%d %d",&u,&v);
47             add(u,v);
48         }
49         tarjan(1,-1);
50         printf("Case %d: %d\n",cs++,ans);
51     }
52     return 0;
53 }
View Code

 

转载于:https://www.cnblogs.com/crackpotisback/p/4394102.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值