您的位置:首页 > 大数据 > 人工智能

HDU 3394 Railway 点双连通分量

2017-08-07 19:58 281 查看


Railway

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2762    Accepted Submission(s): 943


Problem Description

There are some locations in a park, and some of them are connected by roads. The park manger needs to build some railways along the roads, and he would like to arrange tourist routes to each circuit. If a railway belongs to more than one tourist routes, there
might be clash on it, and if a railway belongs to none tourist route, it doesn’t need to build.

Now we know the plan, and can you tell us how many railways are no need to build and how many railways where clash might happen.

 

Input

The Input consists of multiple test cases. The first line of each test case contains two integers, n (0 < n <= 10000), m (0 <= m <= 100000), which are the number of locations and the number of the railways. The next m lines, each line contains two integers,
u, v (0 <= u, v < n), which means the manger plans to build a railway on the road between u and v.

You can assume that there is no loop and no multiple edges.

The last test case is followed by two zeros on a single line, which means the end of the input.

 

Output

Output the number of railways that are no need to build, and the number of railways where clash might happen. Please follow the format as the sample.

 

Sample Input

8 10
0 1
1 2
2 3
3 0
3 4
4 5
5 6
6 7
7 4
5 7
0 0

 

Sample Output

1 5
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stack>
#include<vector>
using namespace std;
const int maxm = 10005;
struct node
{
int x, y;
};
int ans1, ans2, id, dfn[maxm], low[maxm], vis[maxm], n;
stack<node>s;
vector<int>v[maxm];
void init();
void tarjan(int k, int pre);
void judge(int u, int v);
int main()
{
int i, j, k, sum, x, y, m;
while (scanf("%d%d", &n, &m), n != 0)
{
init();
for (i = 1;i <= m;i++)
{
scanf("%d%d", &x, &y);
v[x].push_back(y);
v[y].push_back(x);
}
for (i = 0;i < n;i++)
if (!dfn[i]) tarjan(i, -1);
printf("%d %d\n", ans1, ans2);
}
return 0;
}
void init()
{
for (int i = 0;i < n;i++)
v[i].clear();
memset(dfn, 0, sizeof(dfn));
memset(low, 0, sizeof(low));
ans1 = ans2 = id = 0, id = 1;
}
void tarjan(int k, int pre)
{
dfn[k] = low[k] = id++;
for (int i = 0;i < v[k].size();i++)
{
int xx = v[k][i];
if (xx == pre) continue;
if (!dfn[xx] && dfn[xx] < dfn[k])
{
node temp;
temp.x = k, temp.y = xx;
s.push(temp);
tarjan(xx, k);
low[k] = min(low[k], low[xx]);
if (dfn[k] <= low[xx])
{
if (dfn[k] < low[xx]) ans1++;
judge(k, xx);
}
}
else if (dfn[xx] < dfn[k])
{
node temp;
temp.x = k, temp.y = xx;
s.push(temp);
low[k] = min(low[k], dfn[xx]);
}
}
}
void judge(int u, int v)
{
int ans = 0, rev = 0;
memset(vis, 0, sizeof(vis));
while (!s.empty())
{
node temp = s.top();s.pop();
ans++;
if (!vis[temp.y]) { rev++, vis[temp.y] = 1; }
if (temp.x == u) break;
if (!vis[temp.x]) { rev++, vis[temp.x] = 1; }
}
if (ans > rev) ans2 += ans;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: