Tarjan UVALive 6511 Term Project

题目传送门

 1 /*
 2     题意:第i个人选择第a[i]个人,问组成强联通分量(自己连自己也算)外还有多少零散的人
 3     有向图强联通分量-Tarjan算法:在模板上加一个num数组,记录每个连通分量的点数,若超过1,则将连通点数相加
 4         用总点数-ans则是零散的点
 5     详细解释:http://www.bubuko.com/infodetail-927304.html
 6 */
 7 #include <cstdio>
 8 #include <cstring>
 9 #include <algorithm>
10 #include <vector>
11 #include <stack>
12 using namespace std;
13
14 const int MAXN = 1e5 + 10;
15 const int INF = 0x3f3f3f3f;
16 vector<int> G[MAXN];
17 stack<int> S;
18 int pre[MAXN], low[MAXN];
19 int instack[MAXN], num[MAXN];
20 int dfs_clock, scc_cnt;
21 int n, ans;
22
23 void DFS(int u)
24 {
25     instack[u] = 1;
26     pre[u] = low[u] = ++dfs_clock;
27     S.push (u);
28     for (int i=0; i<G[u].size (); ++i)
29     {
30         int v = G[u][i];
31         if (!pre[v])
32         {
33             DFS (v);    low[u] = min (low[u], low[v]);
34         }
35         else if (instack[v] == 1)
36         {
37             low[u] = min (low[u], pre[v]);
38         }
39     }
40
41     if (low[u] == pre[u])
42     {
43         scc_cnt++;
44         for (; ; )
45         {
46             int x = S.top ();    S.pop ();
47             instack[x] = 0;
48             num[scc_cnt]++;
49             if (x == u)    break;
50         }
51     }
52 }
53
54 void Tarjan(void)
55 {
56     dfs_clock = scc_cnt = 0;
57     memset (pre, 0, sizeof (pre));
58     memset (low, 0, sizeof (low));
59     memset (num, 0, sizeof (num));
60     memset (instack, 0, sizeof (instack));
61     while (!S.empty ())    S.pop ();
62
63     for (int i=1; i<=n; ++i)
64     {
65         if (!pre[i])    DFS (i);
66     }
67 }
68
69 int main(void)        //UVALive 6511 Term Project
70 {
71     freopen ("L.in", "r", stdin);
72
73     int t;    scanf ("%d", &t);
74     while (t--)
75     {
76         ans = 0;    scanf ("%d", &n);
77         for (int i=1; i<=n; ++i)    G[i].clear ();
78         for (int i=1; i<=n; ++i)
79         {
80             int x;    scanf ("%d", &x);
81             G[i].push_back (x);
82             if (i == x)    ans++;
83         }
84
85         Tarjan ();
86         for (int i=1; i<=scc_cnt; ++i)
87         {
88             if (num[i] > 1)    ans += num[i];
89         }
90         printf ("%d\n", n - ans);
91     }
92
93     return 0;
94 }
时间: 2024-10-14 15:32:19

Tarjan UVALive 6511 Term Project的相关文章

UVALive 6511 Term Project

Term Project Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Original ID: 651164-bit integer IO format: %lld      Java class name: Main 解题:强连通分量 1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 1

(Your)((Term)((Project)))

Description You have typed the report of your term project in your personal computer. There are several one line arithmetic expressions in your report. There is no redundant parentheses in the expressions (omitting a pair of redundant matching parent

Regionals 2013 Asia - Daejeon (部分题目题解)

题目链接:Regionals 2013 Asia - Daejeon 6500 Boxes 题意:将箱子(矩阵的1)全移动到矩阵的底部需要几步 思路:按列从下到上统计.(n,m)的矩阵,移动一个箱子(x,y),如果有c个箱子在底部,那么移动该箱子的步数是(n-x-c-1). AC代码: #include <stdio.h> #include <string.h> int mp[110][110]; int main() { int t; int i,j,n,m; scanf(&qu

Dynare/Matlab Project International Finance

Dynare/Matlab ProjectInternational Finance (Open Economy Macroeconomics)NOTE: “Handing in solutions to this project” involves scanning and emailing your analyticalsolutions (to be written in directly in this handout), emailing me the Dynare code(.mod

POJ百道水题列表

以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight Moves1101 Gamblers1204 Additive equations 1221 Risk1230 Legendary Pokemon1249 Pushing Boxes 1364 Machine Schedule1368 BOAT1406 Jungle Roads1411 Annive

分布式系统学习

两个帖子:知乎, Quora @严林 推荐的三篇论文 1.Sinfonia: A New Paradigm for Building Scalable Distributed Systems,这篇论文是SOSP2007的Best Paper,阐述了一种构建分布式文件系统的范式方法,个人感觉非常有用.淘宝在构建TFS.OceanBase和Tair这些系统时都充分参考了这篇论文. 2. The Chubby lock service for loosely-coupled distributed s

poj 动态规划题目列表及总结

此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276,1322, 1414, 1456, 1458, 1609, 1644, 1664, 1690, 1699, 1740(博弈),1742, 1887, 1926(马尔科夫矩阵,求平衡), 1936, 1952, 1953, 1958, 1959, 1962, 1975,

Soj题目分类

-----------------------------最优化问题------------------------------------- ----------------------常规动态规划  SOJ1162 I-Keyboard  SOJ1685 Chopsticks SOJ1679 Gangsters SOJ2096 Maximum Submatrix  SOJ2111 littleken bg SOJ2142 Cow Exhibition  SOJ2505 The County

zoj题目分类

饮水思源---zoj 转载自:http://bbs.sjtu.edu.cn/bbscon,board,ACMICPC,file,M.1084159773.A.html 注:所有不是太难的题都被归成了“简单题”,等到发现的时候已经太晚了,我太死脑筋 了……:( 有些题的程序我找不到了,555……:( SRbGa的题虽然都很经典……但是由于其中的大部分都是我看了oibh上的解题报告后做 的,所以就不写了…… 题目排列顺序没有规律……:( 按照个人感觉,最短路有的算做了DP,有的算做了图论. 有些比较