当前位置: 首页 > news >正文

poj 2186

poj 2186
tarjan缩点板子题
缩完点之后是有向无环图,要想其他点都能到达,那么它的出度为0,不然随意连就会出现环,如果出度为0的点的个数不是1,肯定就凉了。

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<queue>
  4 #include<algorithm>
  5 #include<cmath>
  6 #include<ctime>
  7 #include<set>
  8 #include<map>
  9 #include<stack>
 10 #include<cstring>
 11 #define inf 2147483647
 12 #define ls rt<<1
 13 #define rs rt<<1|1
 14 #define lson ls,nl,mid,l,r
 15 #define rson rs,mid+1,nr,l,r
 16 #define N 100010
 17 #define For(i,a,b) for(int i=a;i<=b;i++)
 18 #define p(a) putchar(a)
 19 #define g() getchar()
 20 
 21 using namespace std;
 22 int n,m,x,y,now;
 23 int dfn[10010],low[10010],ans[10010];
 24 int cnt,col,c[10010],d[10010];
 25 bool vis[10010];
 26 struct node{
 27     int n;
 28     node *next;
 29 }*e[500100];
 30 
 31 stack<int>s;
 32 
 33 void in(int &x){
 34     int y=1;
 35     char c=g();x=0;
 36     while(c<'0'||c>'9'){
 37         if(c=='-')y=-1;
 38         c=g();
 39     }
 40     while(c<='9'&&c>='0'){
 41         x=(x<<1)+(x<<3)+c-'0';c=g();
 42     }
 43     x*=y;
 44 }
 45 void o(int x){
 46     if(x<0){
 47         p('-');
 48         x=-x;
 49     }
 50     if(x>9)o(x/10);
 51     p(x%10+'0');
 52 }
 53 
 54 void push(int x,int y){
 55     node *p;
 56     p=new node();
 57     p->n=y;
 58     if(e[x]==NULL)
 59         e[x]=p;
 60     else{
 61         p->next=e[x]->next;
 62         e[x]->next=p;
 63     }
 64 }
 65 
 66 void tarjan(int x){
 67     dfn[x]=low[x]=++cnt;
 68     vis[x]=true;
 69     s.push(x);
 70     for(node *i=e[x];i;i=i->next){
 71         if(!dfn[i->n]){
 72             tarjan(i->n);
 73             low[x]=min(low[x],low[i->n]);
 74         }
 75         else
 76             if(vis[i->n])
 77                 low[x]=min(low[x],dfn[i->n]);
 78     }
 79 
 80     if(low[x]==dfn[x]){
 81         col++;
 82         do{
 83             ans[col]++;
 84             now=s.top();
 85             c[now]=col;
 86             s.pop();
 87             vis[now]=true;
 88         }while(x!=now);
 89     }
 90 }
 91 
 92 void dfs(int x){
 93     vis[x]=true;
 94     for(node *i=e[x];i;i=i->next){
 95         if(!vis[i->n]){
 96             if(c[i->n]!=c[x])
 97                 d[c[x]]++;
 98             dfs(i->n);
 99         }
100     }
101 }
102 
103 int main(){
104     in(n);in(m);
105     For(i,1,m){
106         in(x);in(y);
107         push(x,y);
108     }
109     For(i,1,n)
110         if(!dfn[i])
111             tarjan(i);
112 
113     For(i,1,n)vis[i]=false;    
114 
115     For(i,1,n)
116         if(!vis[i])
117             dfs(i);
118     cnt=0;
119     For(i,1,col){
120         if(d[i]==0){
121             x=i;
122             cnt++;
123         }
124         //o(d[i]);p('\n');
125     }
126     if(cnt==1)
127         o(ans[x]);
128     else
129         o(0);
130     return 0;
131 }
View Code

//这个题的代码写得很丑23333333

转载于:https://www.cnblogs.com/war1111/p/10342413.html

相关文章:

  • 好男人找不到女朋友的原因
  • 浅入深出Vue:前言
  • 交换两个变量
  • 使用open flash chart的BarGlass时遇到的问题
  • 09视图
  • 什么是高并发 ,详细讲解
  • Struts Nested标签
  • Codeforces Global Round 1
  • (转)母版页和相对路径
  • 不修改原数组方法
  • [Mvc]在ASP.NET MVC中使用Repeater
  • django 一对一, 一对多,多对多的领悟
  • [转]ASP.NET Core: Static Files cache control using HTTP Headers
  • VMware HA实战攻略之五VMwareHA测试验收
  • Java的Cloneable接口还有深浅复制
  • 深入了解以太坊
  • [译] 怎样写一个基础的编译器
  • 《Java8实战》-第四章读书笔记(引入流Stream)
  • co.js - 让异步代码同步化
  • CSS魔法堂:Absolute Positioning就这个样
  • eclipse的离线汉化
  • flutter的key在widget list的作用以及必要性
  • java小心机(3)| 浅析finalize()
  • JS 面试题总结
  • laravel5.5 视图共享数据
  • leetcode46 Permutation 排列组合
  • Python中eval与exec的使用及区别
  • VirtualBox 安装过程中出现 Running VMs found 错误的解决过程
  • Xmanager 远程桌面 CentOS 7
  • 阿里云爬虫风险管理产品商业化,为云端流量保驾护航
  • 对超线程几个不同角度的解释
  • 分布式事物理论与实践
  • 悄悄地说一个bug
  • 如何将自己的网站分享到QQ空间,微信,微博等等
  • 视频flv转mp4最快的几种方法(就是不用格式工厂)
  • 算法之不定期更新(一)(2018-04-12)
  • 做一名精致的JavaScripter 01:JavaScript简介
  • FaaS 的简单实践
  • 扩展资源服务器解决oauth2 性能瓶颈
  • 整理一些计算机基础知识!
  • $$$$GB2312-80区位编码表$$$$
  • (4)logging(日志模块)
  • (Java岗)秋招打卡!一本学历拿下美团、阿里、快手、米哈游offer
  • (SpringBoot)第七章:SpringBoot日志文件
  • (独孤九剑)--文件系统
  • (分享)自己整理的一些简单awk实用语句
  • (一)Thymeleaf用法——Thymeleaf简介
  • (转)EXC_BREAKPOINT僵尸错误
  • (转)关于如何学好游戏3D引擎编程的一些经验
  • .net MySql
  • .NET WebClient 类下载部分文件会错误?可能是解压缩的锅
  • .Net开发笔记(二十)创建一个需要授权的第三方组件
  • .net开发时的诡异问题,button的onclick事件无效
  • .net图片验证码生成、点击刷新及验证输入是否正确
  • @font-face 用字体画图标