HDU 3749 Financial Crisis 点双连通分量
2016-02-18 23:28
337 查看
询问两点间是不联通,还是只有一条路,还是有多条路。
不联通用并查集处理即可。
只有一条路说明经过了割点,求一次点双联通分量,多条路说明在点双连通分量内。
Total Submission(s): 301 Accepted Submission(s): 92
have to turn to the enterprise with stably developing for help.
Nowadays, there exist a complex net of financial trade relationship between enterprises. So if one of enterprises on the same financial chain is faced with bankrupt, leading to cashflow’s obstruction, the other enterprises related with it will be influenced as well. At the moment, the foresight entrepreneurs are expected the safer cooperation between enterprises. In this sense, they want to know how many financial chains between some pairs of enterprises are independent with each other. The indepence is defined that if there exist two roads which are made up of enterprises(Vertexs) and their financial trade relations(Edge) has the common start point S and end point T, and expect S and T, none of other enterprise in two chains is included in these two roads at the same time. So that if one of enterpirse bankrupt in one of road, the other will not be obstructed.
Now there are N enterprises, and have M pair of financial trade relations between them, the relations are Mutual. They need to ask about Q pairs of enterprises’ safety situations. When two enterprises have two or more independent financial chains, we say they are safe enough, you needn’t provide exact answers.
The next M lines, each line contains two integers, u, v ( 0 <= u, v < N && u != v ), which means enterpirse u and enterprise v have trade relations, you can assume that the input will not has parallel edge.
The next Q lines, each line contains two integers, u, v ( 0 <= u, v < N && u != v ), which means entrepreneurs will ask you the financial safety of enterpirse u and enterprise v.
The last test case is followed by three zeros on a single line, which means the end of the input.
0 1
0 2
1 0
4 4 2
0 1
0 2
1 2
2 3
1 2
1 3
0 0 0
zero
one
Case 2:
two or more
one
不联通用并查集处理即可。
只有一条路说明经过了割点,求一次点双联通分量,多条路说明在点双连通分量内。
#include <cstdio> #include <cstring> #include <set> #include <algorithm> #define FOR(i,j,k) for(i=j;i<=k;i++) using namespace std; const int N = 5005, M = 20005; int dfn , low , belong , out , bcc; int fa , top, ts; int h , p[M], v[M], cnt; pair<int, int> stack[M]; set<int> bcc_set ; int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); } void merge(int x, int y) { x = find(x); y = find(y); fa[x] = y; } void add(int x, int y) { p[cnt] = h[x]; v[cnt] = y; h[x] = cnt++; } void tarjan(int u, int fa) { dfn[u] = low[u] = ++ts; for (int i = h[u]; i != -1; i = p[i]) { pair<int, int> edge = make_pair(u, v[i]); if (v[i] == fa) continue; if (!dfn[v[i]]) { stack[++top] = edge; tarjan(v[i], u); low[u] = min(low[u], low[v[i]]); if (low[v[i]] >= dfn[u]) { bcc_set[++bcc].clear(); int a, b; do { a = stack[top].first, b = stack[top].second; --top; if (belong[a] != bcc) belong[a] = bcc, bcc_set[bcc].insert(a); if (belong[b] != bcc) belong[b] = bcc, bcc_set[bcc].insert(b); } while (a != u || b != v[i]); } } else if (dfn[v[i]] < dfn[u]) { stack[++top] = edge; low[u] = min(low[u], dfn[v[i]]); } } } int main() { int i, j, x, y, n, m, q, ans, kase = 0; while (scanf("%d%d%d", &n, &m, &q) == 3 && n && m && q) { memset(dfn, 0, sizeof dfn); memset(h, -1, sizeof h); memset(out, 0, sizeof out); memset(belong, 0, sizeof belong); FOR(i,1,n) fa[i] = i; bcc = cnt = top = ts = 0; while (m--) { scanf("%d%d", &x, &y); ++x, ++y; add(x, y); add(y, x); merge(x, y); } FOR(i,1,n) if (!dfn[i]) tarjan(i, 0); printf("Case %d:\n", ++kase); while (q--) { scanf("%d%d", &x, &y); ++x, ++y; if (find(x) != find(y)) puts("zero"); else { FOR(i,1,bcc) if (bcc_set[i].count(x) && bcc_set[i].count(y) && bcc_set[i].size() > 2) { puts("two or more"); i = -1; break; } if (i != -1) puts("one"); } } } return 0; }
Financial Crisis
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 301 Accepted Submission(s): 92
Problem Description
Because of the financial crisis, a large number of enterprises go bankrupt. In addition to this, other enterprises, which have trade relation with the bankrup enterprises, are also faced with closing down. Owing to the market collapse, profit decline and funding chain intense, the debt-ridden entrepreneurshave to turn to the enterprise with stably developing for help.
Nowadays, there exist a complex net of financial trade relationship between enterprises. So if one of enterprises on the same financial chain is faced with bankrupt, leading to cashflow’s obstruction, the other enterprises related with it will be influenced as well. At the moment, the foresight entrepreneurs are expected the safer cooperation between enterprises. In this sense, they want to know how many financial chains between some pairs of enterprises are independent with each other. The indepence is defined that if there exist two roads which are made up of enterprises(Vertexs) and their financial trade relations(Edge) has the common start point S and end point T, and expect S and T, none of other enterprise in two chains is included in these two roads at the same time. So that if one of enterpirse bankrupt in one of road, the other will not be obstructed.
Now there are N enterprises, and have M pair of financial trade relations between them, the relations are Mutual. They need to ask about Q pairs of enterprises’ safety situations. When two enterprises have two or more independent financial chains, we say they are safe enough, you needn’t provide exact answers.
Input
The Input consists of multiple test cases. The first line of each test case contains three integers, N ( 3 <= N <= 5000 ), M ( 0 <= M <= 10000 ) and Q ( 1 <= Q <= 1000 ). which are the number of enterprises, the number of the financial trade relations and the number of queries.The next M lines, each line contains two integers, u, v ( 0 <= u, v < N && u != v ), which means enterpirse u and enterprise v have trade relations, you can assume that the input will not has parallel edge.
The next Q lines, each line contains two integers, u, v ( 0 <= u, v < N && u != v ), which means entrepreneurs will ask you the financial safety of enterpirse u and enterprise v.
The last test case is followed by three zeros on a single line, which means the end of the input.
Output
For each case, output the test case number formated as sample output. Then for each query, output “zero” if there is no independent financial chains between those two enterprises, output “one” if there is only one such chains, or output “two or more”.Sample Input
3 1 20 1
0 2
1 0
4 4 2
0 1
0 2
1 2
2 3
1 2
1 3
0 0 0
Sample Output
Case 1:zero
one
Case 2:
two or more
one
相关文章推荐
- jsp的 javascript中 嵌套 html 注释
- maven 骨架项目
- C#中的线程(一)入门
- 《远大前程 从软件新手到行业大牛》学习总结
- [Lintcode] Matrix Zigzag Traversal
- HDU 4300 Clairewd’s message
- 225. Implement Stack using Queues 用两个队列实现栈的基本操作。
- java.lang.VerifyError
- 文件操作杂记
- 变量的初始化
- Linux笔记(5)——搜索命令
- 内部类基本知识点
- tomcat7+nginx 负载均衡
- 范型,迭代Map
- C++ 自制Redis 数据库(十一)哈希类与数据库类
- ios闭包循环引用精讲
- Sigar 获取CPU和Memory内存等信息使用详解
- CPU和GPU的区别
- Createjs学习心得之使用EaselJs实现拖拽效果
- sql优化 in和exists效率讨论