lightoj1210Efficient Traffic System
来源:互联网 发布:重置sql密码用户名 编辑:程序博客网 时间:2024/05/22 12:42
思路:给定一个有向图,问至少需要添加几条有向边可以使得图成为一个强连通图。
显然是需要缩点的,因为一个环上的点是可以互达的,可以看成一个点,如果图本
身就是强连通的输出0,因为不需要添加。否则就看缩点后每个点的入度与出度。
没有出的,要添加,没有入的要添加,,,这里就去其最大值(因为没有入的可以
和没有出的点之间加边,剩下的就要和其他任何点之间建边了)。
// #pragma comment(linker, "/STACK:1024000000,1024000000")#include <iostream>#include <algorithm>#include <iomanip>#include <sstream>#include <string>#include <stack>#include <queue>#include <deque>#include <vector>#include <map>#include <set>#include <stdio.h>#include <string.h>#include <math.h>#include <stdlib.h>#include <limits.h>// #define DEBUG#ifdef DEBUG#define debug(...) printf( __VA_ARGS__ )#else#define debug(...)#endif#define MEM(x,y) memset(x, y,sizeof x)using namespace std;typedef long long LL;typedef unsigned long long ULL;typedef pair<int,int> ii;const int inf = 1 << 30;const int INF = 0x3f3f3f3f;const int MOD = 1e9 + 7;const int maxn = 20010;int head[maxn], to[maxn << 2], nxt[maxn << 2];int dfn[maxn], low[maxn], belong[maxn];bool vis[maxn];int Time;int scc;int n ,m;stack<int> st;int tol;void add(int u,int v){to[tol] = v;nxt[tol] = head[u];head[u] = tol++;}void Tarjan(int u){dfn[u] = low[u] = Time++;vis[u] = true;st.push(u);for (int i = head[u];i != -1;i = nxt[i]){int v = to[i];if (dfn[v] == -1){Tarjan(v);low[u] = min(low[u], low[v]);}else if (vis[v] && dfn[v] < low[u]){low[u] = dfn[v];}}if (dfn[u] == low[u]){scc++;while(true){int v = st.top();st.pop();vis[v] = false;belong[v] = scc;if (v == u) break;}}}int in[maxn], out[maxn];void dfs(int u){dfn[u] = 1;for (int i = head[u];i != -1;i = nxt[i]){int v = to[i];int a = belong[u];int b = belong[v];if (a != b){in[b]++;out[a]++;}if (dfn[v] == -1) dfs(v);}}int solve(){if (scc == 1) return 0;int a = 0,b = 0;for (int i = 1;i <= scc;++i){if (!in[i]) a++;if (!out[i]) b++;}return max(a,b);}int main(){// freopen("in.txt","r",stdin);// freopen("out.txt","w",stdout);int icase = 0, t;cin >> t;while(t--){scanf("%d%d",&n,&m);tol = scc = 0;Time = 0;MEM(belong, -1);MEM(head, -1);MEM(vis, false);MEM(dfn, -1);for (int i = 0, a, b;i < m;++i){scanf("%d%d",&a, &b);add(a,b);}for (int i = 1;i <= n;++i) if (dfn[i] == -1) Tarjan(i);// cout << "scc = " << scc << endl;MEM(dfn, -1);MEM(in, 0);MEM(out, 0);for (int i = 1;i <= n;++i)if (dfn[i] == -1) dfs(i);printf("Case %d: %d\n", ++icase, solve());}return 0;}
0 0
- lightoj1210Efficient Traffic System
- LightOJ 1210 Efficient Traffic System
- LightOJ - 1281 New Traffic System
- lightoj 1281 - New Traffic System
- lightoj 1210 - Efficient Traffic System
- HDU 5910Advanced Traffic System
- HDU Traffic Real Time Query System
- LightOJ - 1210 Efficient Traffic System(强连通)
- HDU3686 Traffic Real Time Query System
- 4839 -- Traffic Real Time Query System
- [最短路] HDU 5910 Advanced Traffic System
- [强连通分量]LightOJ 1210 - Efficient Traffic System
- lightoj 1281 - New Traffic System (有限制最短路)
- UVALive 4839 HDU 3686 Traffic Real Time Query System
- LightOJ 1210 - Efficient Traffic System【强连通图】
- LightOJ - 1281 New Traffic System(二维最短路)
- UVALive 4839 HDU 3686 Traffic Real Time Query System
- Vehicle detection and recognition for intelligent traffic survelliance system 论文
- 【python】xml Elementtree 学习一
- [js学习笔记]城市选择控件(双下拉框选择不同城市列表)
- LayoutInflater
- 将maven项目自动部署至私有nexus maven仓库
- 【南大软院大神养成计划:第一天】种下21天,收获不一样的自己——前端养成启程!
- lightoj1210Efficient Traffic System
- iOS宏定义
- iOS图文混排
- gmock为什么只能支持虚函数(其实也不准确)
- 对iOS的主要框架的介绍
- HDU 3400 (三分)
- webservice编程中的RPCServiceClient类涉及的jar包
- Http协议理解
- java笔记_正则表达式