题目传送门

 /*
题意:第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. memcached源代码分析-----set命令处理流程

    转载请注明出处:http://blog.csdn.net/luotuo44/article/details/44236591 前一篇博文以get命令为样例把整个处理流程简单讲述了一遍.本篇博文将以se ...

  2. 手机没Root?你照样可以渗透路由器

    和Metasploit差不多,RouterSploit是一个强大的漏洞利用框架,用于快速识别和利用路由器中的普通漏洞,它还有个亮点,就是可以在绝大多数安卓设备上运行. 如果你想在电脑上运行,可以阅读这 ...

  3. CEF3研究(二)

    应用程序结构 每个CEF3应用程序都有一个相同的结构: 提供一个入口函数以初始化CEF和运行每个子进程逻辑和CEF消息处理 提供一个CefApp子类处理某个进程的回调 提供一个CefClinet子类处 ...

  4. 【effective c++】模板与泛型编程

    模板元编程:在c++编译器内执行并于编译完成时停止执行 1.了解隐式接口和编译期多态 面向对象编程总是以显式接口(由函数名称.参数类型和返回类型构成)和运行期多态(虚函数)解决问题 模板及泛型编程:对 ...

  5. [RxJS] Chain RxJS Operators Together with a Custom `pipe` Function using Array.reduce

    Instead of writing complex operators, it's usually best to write simple, single-purpose operators th ...

  6. unix时间戳(unix timestamp)与北京时间的互转方法

    1.在linux bash下北京时间与unix时间戳互转: 获取unix timestamp: 命令:date "+%s" 输出:1372654714 获取北京时间: 命令:dat ...

  7. libevent API 介绍

    基本应用场景也是使用 libevnet 的基本流程,下面来考虑一个最简单的场景,使用livevent 设置定时器,应用程序只需要执行下面几个简单的步骤即可. 1)首先初始化 libevent 库,并保 ...

  8. JavaScript的高大强

    1,JavaScript的引入方式 1.1>Script标签内写代码 <Script> //这里写JS代码的地方 </Script> 1.2>引入额外的JS文件 & ...

  9. cocos2dx笔记1:概述

    1.核心的类和功能 CCDirector gameLoop,实现场景绘制.多个场景之间切换控制.控制游戏的停止,暂停,等生命周期. CCScene 场景类,每一个场景能够理解为一个游戏镜头.状态 CC ...

  10. C # 踩坑记录(20190603)

    由于公司战略层需求,需要学习c#,在此仅记录相关问题,以便后期回顾. 学习路线 .NET 框架学习与C # 的关系 Visual Studio 简介及相关帮助网站(msdn) Main 方法及&quo ...