Term Project

Time Limit: 3000ms
Memory Limit: 131072KB

This problem will be judged on UVALive. Original ID: 6511
64-bit integer IO format: %lld      Java class name: Main

解题:强连通分量 

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 100010;
 4 struct arc{
 5     int to,next;
 6     arc(int x = 0,int y = -1){
 7         to = x;
 8         next = y;
 9     }
10 }e[maxn*10];
11 int head[maxn],dfn[maxn],low[maxn],belong[maxn],cnt[maxn];
12 int tot,clk,scc;
13 bool instack[maxn];
14 stack<int>stk;
15 void init(){
16     for(int i = tot = clk = scc = 0; i < maxn; ++i){
17         head[i] = -1;
18         dfn[i] = 0;
19         cnt[i] = belong[i] = 0;
20         instack[i] = false;
21     }
22     while(!stk.empty()) stk.pop();
23 }
24 void add(int u,int v){
25     e[tot] = arc(v,head[u]);
26     head[u] = tot++;
27 }
28 void tarjan(int u){
29     dfn[u] = low[u] = ++clk;
30     instack[u] = true;
31     stk.push(u);
32     for(int i = head[u]; ~i; i = e[i].next){
33         if(!dfn[e[i].to]){
34             tarjan(e[i].to);
35             low[u] = min(low[u],low[e[i].to]);
36         }else if(instack[e[i].to]) low[u] = min(low[u],dfn[e[i].to]);
37     }
38     if(low[u] == dfn[u]){
39         scc++;
40         int v;
41         do{
42             instack[v = stk.top()] = false;
43             belong[v] = scc;
44             stk.pop();
45             cnt[scc]++;
46         }while(v != u);
47     }
48 }
49 int main(){
50     int kase,n;
51     scanf("%d",&kase);
52     while(kase--){
53         init();
54         scanf("%d",&n);
55         int ret = 0;
56         for(int i = 1,tmp; i <= n; ++i){
57             scanf("%d",&tmp);
58             add(i,tmp);
59             ret += i == tmp;
60         }
61         for(int i = 1; i <= n; ++i)
62             if(!dfn[i]) tarjan(i);
63         for(int i = 1; i <= scc; ++i)
64             if(cnt[i] > 1) ret += cnt[i];
65         printf("%d\n",n - ret);
66     }
67     return 0;
68 }

View Code

转载于:https://www.cnblogs.com/crackpotisback/p/4855004.html

UVALive 6511 Term Project相关推荐

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

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

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

    题目链接:Regionals 2013 Asia - Daejeon 6500 Boxes 题意:将箱子(矩阵的1)全移动到矩阵的底部需要几步 思路:按列从下到上统计.(n,m)的矩阵,移动一个箱子( ...

  3. Project Management

    Project Management 文章目录 Project Management 1. Intruduction 2. Organization 3. Project Management Pro ...

  4. 【转】别人整理的DP大全

    为什么80%的码农都做不了架构师?>>>    动态规划 动态规划 容易: 1018 , 1050 , 1083 , 1088 , 1125 , 1143 , 1157 , 1163 ...

  5. it职业生涯_如何通过这些有用的网站提升您的自由职业生涯

    it职业生涯 According to a recent report released by the Freelancer's Union, in 2015 there are approximat ...

  6. Stanford University courses of computer science department(斯坦福计算机系课程设置)

    斯坦福学科目前分为7个department:Business, Earth, Education, Engineering, Humanities & Sciences, Law, Medic ...

  7. 狗狗40题~ (Volume C)

    A - Triangles 记忆化搜索呗.搜索以某三角形为顶的最大面积,注意边界情况. 1 #include <stdio.h> 2 #include <cstring> 3 ...

  8. 知识点滴 - 项目阶段的定义

    项目阶段的定义 术语项目阶段指的是项目中一些活动的集合.每个项目阶段都以目标为导向,并在一个里程碑处结束.达到这些里程碑意味着项目的进展.每个阶段都可以划分为子阶段.项目计划(例如,项目网络图)明确了 ...

  9. POJ的题目分类(两个版本)

    版本一: 简单题 1000A+B Problem 1001Exponentiation 1003 Hangover 1004 Financial Management 1005 I Think I N ...

最新文章

  1. rdp协议打开 windows_RDPY - Twisted Python 实现的RDP协议(Windows 远程桌面)
  2. 销售员/学员/讲师系统
  3. geoserver动态颜色参数样式、动态sql配置实现
  4. [POJ 1330] Nearest Common Ancestors (倍增法)
  5. Visual studio之C# 利用Settings保存COM口配置信息
  6. cf723d Lakes in Berland
  7. Powershell管理系列(一)Active Direcrtory管理:用户管理
  8. c语言程序设计学到了什么,C语言程序设计入门学习六步曲
  9. Xshell 5 Build 1339 (5.0.1339)
  10. 读《如何找对另一半》后感以及论自己择偶标准
  11. 计算机的基本储存单元电路,DRAM的存储单元电路.PPT
  12. 大陆、香港、澳门、台湾身份证最全正则校验
  13. 【第三届蓝桥杯】手机尾号
  14. Eureka的自我保护机制与如何禁用
  15. button 点击的涟漪效果
  16. php取微信名字和头像,微信小程序如何获取用户头像和昵称
  17. 人工智能之集束搜索Beam Search Algorithm
  18. 用c++实现贪吃蛇小游戏,初学者记录一下首次实现的经历,有超详细的思路与语法讲解,新手向
  19. percona-tool文档说明(4)- 监控类
  20. class中的static

热门文章

  1. java ajax 导入excel_Ajax asp.net 导入Excel
  2. aosp 本地版本管理_谈 DevOps 平台实施:我在本地跑明明成功的,为什么在你平台跑就报错?...
  3. myeclipse怎么导入mysql驱动_myeclipse sql导入数据库驱动包
  4. matlab不同调制方式下性能比较,用不同调制方式实现跳/扩频混合通信的抗干扰性能...
  5. leetcode453. 最小操作次数使数组元素相等(贼难的简单题)
  6. leetcode80. 删除排序数组中的重复项 II
  7. redis——缓存击穿/穿透/雪崩
  8. 李牛(Linux)打包
  9. C++(15)--面向对象编程实践-欢乐斗地主(vector的花式输出)
  10. Tensorflow(0)--Tensorboard