题目传送门

 /*
题意:第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的更多相关文章

  1. UVALive 6511 Term Project

    Term Project Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Origi ...

  2. (Your)((Term)((Project)))

    Description You have typed the report of your term project in your personal computer. There are seve ...

  3. POJ--1690 (Your)((Term)((Project)))(字符串处理)

    (Your)((Term)((Project))) Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 3353 Accepted: ...

  4. POJ 1690 (Your)((Term)((Project)))

    (Your)((Term)((Project))) Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2912   Accept ...

  5. ZOJ 1423 (Your)((Term)((Project))) (模拟+数据结构)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=423 Sample Input 3(A-B + C) - (A+(B ...

  6. Storm(3) - Calculating Term Importance with Trident

    Creating a URL stream using a Twitter filter Start by creating the project directory and standard Ma ...

  7. Distributed Databases and Data Mining: Class timetable

    Course textbooks Text 1: M. T. Oszu and P. Valduriez, Principles of Distributed Database Systems, 2n ...

  8. 别人整理的DP大全(转)

    动态规划 动态规划 容易: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ...

  9. dp题目列表

    此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 11 ...

随机推荐

  1. I2S简单学习

    以下只是个人看法,有不妥之处,请批评指出. 参考资料:http://blog.csdn.net/ce123_zhouwei/article/details/6919954: 一.I2S接口简述 I²S ...

  2. 4 自动化构建工具gulp

    gulp中文网:http://www.gulpjs.com.cn/ 需要全局安装gulp:$ npm install --global gulp 具体的gulp的使用方法,可以参看gulp中文网的文档 ...

  3. lmhostid获取hostid为空问题

    lmhostid获取hostid为空问题 问题描写叙述 今天迁移曾经的一个装有flexlm的虚拟机,结果发如今迁移后启动时报错 ... Wrong hostid on SERVER line for ...

  4. 嵌入式驱动开发之---dm8127 中sensor 驱动的改变

    #IPNC_DEVICE := DM385IPNC_DEVICE := DM812x # Values are "LOW_POWER" and "FULL_FEATURE ...

  5. C编程中fread 、fwrite 用法总结

    在C语言中进行文件操作时,我们经常用到fread()和fwrite(),用它们来对文件进行读写操作.下面详细绍一下这两个函数的用法.   我们在用C语言编写程序时,一般使用标准文件系统,即缓冲文件系统 ...

  6. 交换分区 在dd命令执行期间 top 其消耗系统约14%的cpu,而mem占比约为0

    [资源不友好代码] from pyltp import * d_dir = '/usr/local/ltp_data_v3.4.0/' def gen_one_sentence_part(paragr ...

  7. JDBC 详解

    工作原理流程:装载驱动程序---->获得数据库连接---->使用Statement或PreparedStatement执行SQL语句----> 返回执行的结果---->关闭相关 ...

  8. HTTP要点概述:三,客户端和服务器,请求和响应

    一,客户端和服务器: HTTP协议主要用于客户端和服务器之间的通信. 1,客户端(client):请求访问资源的一端.(知道为啥用C表示客户端了吧) 2,服务器(server):提供资源响应的一端. ...

  9. log4j_自定义样式参数意义

    #自定义样式 %c 输出所属的类目,通常就是所在类的全名 %C 输出Logger所在类的名称,通常就是所在类的全名 %d 输出日志时间点的日期或时间,默认格式为ISO8601,也可以在其后指定格式,比 ...

  10. 关于页面上输入框中 空格 、0 、NULL 的处理 示例

    ep.setPositionNum(get("positionNum").toString()); ep.setClasstype(get("classtype" ...