Tarjan UVALive 6511 Term Project
/*
题意:第i个人选择第a[i]个人,问组成强联通分量(自己连自己也算)外还有多少零散的人
有向图强联通分量-Tarjan算法:在模板上加一个num数组,记录每个连通分量的点数,若超过1,则将连通点数相加
用总点数-ans则是零散的点
详细解释:http://www.bubuko.com/infodetail-927304.html
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <stack>
using namespace std; const int MAXN = 1e5 + ;
const int INF = 0x3f3f3f3f;
vector<int> G[MAXN];
stack<int> S;
int pre[MAXN], low[MAXN];
int instack[MAXN], num[MAXN];
int dfs_clock, scc_cnt;
int n, ans; void DFS(int u)
{
instack[u] = ;
pre[u] = low[u] = ++dfs_clock;
S.push (u);
for (int i=; i<G[u].size (); ++i)
{
int v = G[u][i];
if (!pre[v])
{
DFS (v); low[u] = min (low[u], low[v]);
}
else if (instack[v] == )
{
low[u] = min (low[u], pre[v]);
}
} if (low[u] == pre[u])
{
scc_cnt++;
for (; ; )
{
int x = S.top (); S.pop ();
instack[x] = ;
num[scc_cnt]++;
if (x == u) break;
}
}
} void Tarjan(void)
{
dfs_clock = scc_cnt = ;
memset (pre, , sizeof (pre));
memset (low, , sizeof (low));
memset (num, , sizeof (num));
memset (instack, , sizeof (instack));
while (!S.empty ()) S.pop (); for (int i=; i<=n; ++i)
{
if (!pre[i]) DFS (i);
}
} int main(void) //UVALive 6511 Term Project
{
freopen ("L.in", "r", stdin); int t; scanf ("%d", &t);
while (t--)
{
ans = ; scanf ("%d", &n);
for (int i=; i<=n; ++i) G[i].clear ();
for (int i=; i<=n; ++i)
{
int x; scanf ("%d", &x);
G[i].push_back (x);
if (i == x) ans++;
} Tarjan ();
for (int i=; i<=scc_cnt; ++i)
{
if (num[i] > ) ans += num[i];
}
printf ("%d\n", n - ans);
} return ;
}
Tarjan UVALive 6511 Term Project的更多相关文章
- UVALive 6511 Term Project
Term Project Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Origi ...
- (Your)((Term)((Project)))
Description You have typed the report of your term project in your personal computer. There are seve ...
- POJ--1690 (Your)((Term)((Project)))(字符串处理)
(Your)((Term)((Project))) Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 3353 Accepted: ...
- POJ 1690 (Your)((Term)((Project)))
(Your)((Term)((Project))) Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2912 Accept ...
- ZOJ 1423 (Your)((Term)((Project))) (模拟+数据结构)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=423 Sample Input 3(A-B + C) - (A+(B ...
- Storm(3) - Calculating Term Importance with Trident
Creating a URL stream using a Twitter filter Start by creating the project directory and standard Ma ...
- Distributed Databases and Data Mining: Class timetable
Course textbooks Text 1: M. T. Oszu and P. Valduriez, Principles of Distributed Database Systems, 2n ...
- 别人整理的DP大全(转)
动态规划 动态规划 容易: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ...
- dp题目列表
此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 11 ...
随机推荐
- Oracle 数据库管理员的任务
设计.实施和维护 Oracle 数据库时,按优先次序排列的任务包括: 1. 确定数据库服务器硬件 2. 安装 Oracle 软件 3. 为数据库和安全策略制定计划 4. 创建.移植和打 ...
- 单点登录cas常见问题(二) - 子系统是否会频繁訪问cas中心?
这个问题的完整描写叙述是:用户成功登陆后.在訪问子系统的受限资源时,还须要訪问cas中心么,即子系统是否还会频繁訪问cas中心.cas中心会不会压力太大? 答案是:不会. 假设用户通过子系统A登录了c ...
- Native进程之Trace原理(转)——可直接输出某进程的栈帧——debuggerd
一. 概述 当发生ANR(Application Not Response,对于Java进程可通过kill -3向目标进程发送信号SIGNAL_QUIT, 输出相应的traces信息保存到目录/dat ...
- IEnumerator<TItem>和IEnumerator Java 抽象类和普通类、接口的区别——看完你就顿悟了
IEnumerable 其原型至少可以说有15年历史,或者更长,它是通过 IEnumerator 来定义的,而后者中使用装箱的 object 方式来定义,也就是弱类型的.弱类型不但会有性能问题,最主要 ...
- ZOJ 3228 Searching the String (AC自己主动机)
题目链接:Searching the String 解析:给一个长串.给n个不同种类的短串.问分别在能重叠下或者不能重叠下短串在长串中出现的次数. 能重叠的已经是最简单的AC自己主动机模板题了. 不能 ...
- js和jquery实现回到顶层
js <!DOCTYPE html> <html> <head> <title>返回顶部</title> <style> bod ...
- 链接脚本在编程中的高级运用之二——执行时库和C++特性支持
我们在链接脚本在编程中的高级运用之中的一个可变长数组中已经讲述了编译链接的原理,并且以uboot命令为例具体介绍链接脚本怎样实现可变长数组. 本章在前者的基础上继续讲述链接脚本在执行时库中的高级应用技 ...
- grep命令最经常使用的功能总结
1. grep最简单的用法,匹配一个词:grep word filename 2. 能够从多个文件里匹配:grep word filename1 filenam2 filename3 3. 能够使用正 ...
- Receiver type ‘X’ for instance message is a forward declaration
这往往是引用的问题. ARC要求完整的前向引用,也就是说在MRC时代可能仅仅须要在.h中申明@class就能够,可是在ARC中假设调用某个子类中未覆盖的父类中的方法的话.必须对父类.h引用,否则无法编 ...
- ubuntu安装jdk 1.6
linux下安装JDK1.6 1. 去http://java.sun.com/j2se/1.4.2/download.html 下载一个Linux Platform的JDK,建议下载RPM自解压格式的 ...