题目传送门

 /*
题意:第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. coco2d-js demo程序之滚动的小球

    近期有一个游戏叫围住神经猫,报道说是使用html5技术来做的. html5的跨平台的优良特性非常不错.对于人手不足,技术不足,选用html5技术实现跨平台的梦想真是不错. 近期在看coco2d-js这 ...

  2. 【Android开发-4】进入实践,最喜欢折腾的计算器

    前言:前面对项目文件有了感性认识.接下来我们就须要通过不断实践,对项目的文件有理性的认识. 曾经折腾Unity3d.IOS开发都是拿计算器开刀.所以这次Android开发实践也不例外,继续拿计算器折腾 ...

  3. C实现头插法和尾插法来构建单链表(不带头结点)

    链表的构建事实上也就是不断插入节点的过程.而节点的插入能够分为头插法和尾插法. 头插法就是在头结点后插入该节点,始终把该节点作为第一个节点.尾插法就是在链表的最后一个节点处插入元素,作为最后一个节点. ...

  4. java实用技能 上传文件 等等

    1.IOS  AES对称加密,加密结果不同,问题解决 IOS http post请求,使用AFNetworing 框架,默认请求content-type为application/json ,所以无法使 ...

  5. jsp_类的封装_集合的应用

    一.需求分析 做一个jsp页面,动态显示信息表的内容. 1.做一个实体类:StudentInfo(包含4个字段) 2.如图模拟生成3条数据,本质上就是new StudentInfo 3个实例, 每一个 ...

  6. 嵌入式开发之davinci--- mcfw框架介绍

    整体上mcfw框架如下图 从中可见其层次是清楚的,link实在基本的驱动之上的,而mcfw是在link之上的api,是通过link来实现相应的功能.可见link是框架中承上启下的层次,通过link来实 ...

  7. BestCoder #49 Untitled HDU 5339

    BestCoder #49 Untitled  HDU 5339 题目: http://acm.hdu.edu.cn/showproblem.php? pid=5339 本题採用深搜, 数据量小,先做 ...

  8. HDU 5544 Ba Gua Zhen dfs+高斯消元

    Ba Gua Zhen Problem Description During the Three-Kingdom period, there was a general named Xun Lu wh ...

  9. Tomcat最多支持并发多少用户?

    当一个进程有 500 个线程在跑的话,那性能已经是很低很低了.Tomcat 默认配置的最大请求数是 150,也就是说同时支持 150 个并发,当然了,也可以将其改大.当某个应用拥有 250 个以上并发 ...

  10. File to byte[] in Java

    File to byte[] in Java - Stack Overflow https://stackoverflow.com/questions/858980/file-to-byte-in-j ...