[codeforces] Gym
来源:互联网 发布:nokia5310xm软件下载 编辑:程序博客网 时间:2024/06/06 00:39
[codeforces] Gym - 101246G Revolutionary Roads (DFS)
题目链接:
Revolutionary Roads
题目大意:
给n个点和m条有向边, 你可以把一条有向边变成双向边, 问改完之后形成的最大强连通分量有多大,并且输出所有能改的边。
数据范围:
保证不会出现自环
解题思路:
比赛的时候一直往强连通分量上面去想了。 到最后也没写出来, 之后才发现数据这么小, 可以暴力跑出每个点能到达的点。
那么对于一条边来说, 当前连着两个点
总复杂度为
要注意的是, 有可能没有边, 这时候最大之应该为1, 一个点自己也是强连通
代码:
#include<cstdio>#include<cstring>#include<cmath>#include<cstdlib>#include<algorithm>#include<queue>#include<vector>using namespace std;typedef long long LL;const int MaxN = 1000, MaxV = 2e4;int n, m;vector<int>edge[MaxV + 5];int mp[MaxN + 5][MaxN + 5];vector<int> ans;bool vis[MaxN + 5];struct NODE{ int u, v, cnt;}box[MaxV + 5];void dfs(int u, int root){ mp[u][root] = 1; vis[u] = true; for(int i = 0; i < edge[u].size(); i++){ int v = edge[u][i]; if(!vis[v]) dfs(v, root); }}int main(){ freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); while(~scanf("%d %d", &n, &m)){ for(int i = 1; i <= m; i++) { int u, v; scanf("%d %d", &u, &v); edge[u].push_back(v); box[i].u = u, box[i].v = v, box[i].cnt = 0; } for(int i = 1; i <= n; i++){ mp[i][i] = 1; memset(vis, 0, sizeof(vis)); dfs(i, i); } int Max = 1; for(int i = 1; i <= m; i++){ int u = box[i].u, v = box[i].v; for(int j = 1; j <= n; j++) if((mp[u][j] && mp[j][v]) || (mp[v][j] && mp[j][u])) box[i].cnt++; Max = max(Max, box[i].cnt); } for(int i = 1; i <= m; i++) if(box[i].cnt == Max) ans.push_back(i); printf("%d\n%d\n", Max, ans.size()); for(int i = 0; i < ans.size(); i++) if(i == ans.size() - 1) printf("%d\n", ans[i]); else printf("%d ", ans[i]); memset(edge, 0, sizeof(edge)); memset(mp, 0, sizeof(mp)); memset(box, 0, sizeof(box)); ans.clear(); } return 0;}
标签(空格分隔): codeforces DFS
阅读全文
0 0
- 【Codeforces Gym
- 【codeforces Gym
- codeforces Gym
- codeforces Gym
- codeforces Gym
- codeforces Gym
- Codeforces Gym
- Codeforces Gym
- Codeforces Gym
- codeforces Gym
- Codeforces Gym
- Codeforces Gym
- Codeforces Gym
- [codeforces] Gym
- [codeforces] Gym
- [codeforces] Gym
- [codeforces] Gym
- [codeforces] Gym
- 算法排序之选择排序
- pat:L1-032. Left-pad
- 洛谷P3375 【模板】KMP字符串匹配
- mysql性能优化
- HDU 1069 Monkey and Banana(dp)
- [codeforces] Gym
- frame和iframe的区别
- python入门(十一):函数与函数式编程
- web开发第三方登陆之QQ登陆
- 教你上传本地代码到github
- HDU1466 计算直线的交点数
- 参数替换
- HDU 1730 Northcott Game
- JAVA中int转String类型有三种方法