UVALive 6511 Term Project
2015-10-04 21:01
281 查看
Term Project
Time Limit: 3000msMemory Limit: 131072KB
This problem will be judged on UVALive. Original ID: 6511
64-bit integer IO format: %lld Java class name: Main
#include <bits/stdc++.h> using namespace std; const int maxn = 100010; struct arc{ int to,next; arc(int x = 0,int y = -1){ to = x; next = y; } }e[maxn*10]; int head[maxn],dfn[maxn],low[maxn],belong[maxn],cnt[maxn]; int tot,clk,scc; bool instack[maxn]; stack<int>stk; void init(){ for(int i = tot = clk = scc = 0; i < maxn; ++i){ head[i] = -1; dfn[i] = 0; cnt[i] = belong[i] = 0; instack[i] = false; } while(!stk.empty()) stk.pop(); } void add(int u,int v){ e[tot] = arc(v,head[u]); head[u] = tot++; } void tarjan(int u){ dfn[u] = low[u] = ++clk; instack[u] = true; stk.push(u); for(int i = head[u]; ~i; i = e[i].next){ if(!dfn[e[i].to]){ tarjan(e[i].to); low[u] = min(low[u],low[e[i].to]); }else if(instack[e[i].to]) low[u] = min(low[u],dfn[e[i].to]); } if(low[u] == dfn[u]){ scc++; int v; do{ instack[v = stk.top()] = false; belong[v] = scc; stk.pop(); cnt[scc]++; }while(v != u); } } int main(){ int kase,n; scanf("%d",&kase); while(kase--){ init(); scanf("%d",&n); int ret = 0; for(int i = 1,tmp; i <= n; ++i){ scanf("%d",&tmp); add(i,tmp); ret += i == tmp; } for(int i = 1; i <= n; ++i) if(!dfn[i]) tarjan(i); for(int i = 1; i <= scc; ++i) if(cnt[i] > 1) ret += cnt[i]; printf("%d\n",n - ret); } return 0; }
View Code
相关文章推荐
- Codeforces/gym/100685/problem/G Gadget Hackwrench ( LCA )
- hdu4121 判断黑帅下一步会不会被红棋将军(分类讨论)
- 入门erlang的一些感想。
- 斗争程序猿(三十七)——历史朝代大学(一)——开幕
- iOS9中http不能使用的解决
- charles破解
- iOS oc加载plist数据
- 常用类(String、StringBuffer、Random、Enum,日期类、数字类)
- 【ACM】HDOJ 1045 Fire Net
- 【ACM】HDOJ 1045 Fire Net
- [笔试题目] 腾讯2015年9月基础研究笔试题
- SQLYog快捷键大全
- js中,var 修饰变量名,它可有可无吗?
- Delphi中的注释
- nyoj 915 +-字符串
- UI课程12 模态viewController,单例及单例模式
- BZOJ1880: [Sdoi2009]Elaxia的路线|dijksrtra|暴力
- php访问方法外变量
- bzoj2818
- 国庆出行之路这么堵,大数据为何失灵?