题目传送门

 /*
题意:第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. 利用WiFi Pineapple Nano渗透客户端获取SHELL

    前言: 前两篇文章介绍了The WiFi Pineapple Nano设备的一些主要功能模块,例如PineAP.SSLsplit和Ettercap等.今天给大家实际场景演示下如何利用Pineapple ...

  2. 无限级分类Asp.net Mvc实现

    无限级分类Asp.net Mvc实现   无限级分类涉及到异步加载子类.加载当前类和匹配问题,现在做一个通用的实现.   (一) 效果如下:   (二)设计.实现及使用 (1)数据库 (a)表设计db ...

  3. Ubuntu16.04下安装Tensorflow CPU版本(图文详解)

    不多说,直接上干货! 推荐 全网最详细的基于Ubuntu14.04/16.04 + Anaconda2 / Anaconda3 + Python2.7/3.4/3.5/3.6安装Tensorflow详 ...

  4. 网络知识: 物理层PHY 和 网络层MAC

    PHY模块简介 物理层位于OSI最底层,物理层协议定义电气信号.线的状态.时钟要求.数据编码和数据传输用的连接器. 物理层的器件称为PHY. 上图里的灰色方框图里的就是PHY芯片内部模块图. MAC器 ...

  5. asp.net编码解码的两篇文章

    http://www.cnblogs.com/freshman0216/p/4165949.html http://www.cnblogs.com/freshman0216/p/4172655.htm ...

  6. 【java报错】CacheLoader returned null for key class

    CacheLoader returned null for key class cmd     mvn eclipse:clean eclipse:eclipse mvn install -Dmave ...

  7. C#6.0 新功能

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  8. 迭代器-iteration

    class CoffrrIterator implements Iterator<Coffee> { int cunt = size; public boolean hasNext() { ...

  9. linux网络socket 接口转

    linux网络socket 接口 1.socket函数:一个进程必须做的第一件事就是调用socket函数获得一个文件描述符. ------------------------------------- ...

  10. action 与 action 之间的跳转

    这个要看你的两个action是不是在通一个package下了 <!-- 相同package下调用其它action --><action name="loginUser&qu ...