poj2186(tarjan模板)

不管怎么说板子才是最重要的。。

出现频率是不高啦。。不过遇到就不太好了。。毕竟一是板子变量太多容易混,二是重建图相当麻烦。。

重建图方面这里用了把原图的节点编号全部+n的方法。。




/**
 *        ┏┓    ┏┓
 *        ┏┛┗━━━━━━━┛┗━━━┓
 *        ┃       ┃  
 *        ┃   ━    ┃
 *        ┃ >   < ┃
 *        ┃       ┃
 *        ┃... ⌒ ...  ┃
 *        ┃       ┃
 *        ┗━┓   ┏━┛
 *          ┃   ┃ Code is far away from bug with the animal protecting          
 *          ┃   ┃   神兽保佑,代码无bug
 *          ┃   ┃           
 *          ┃   ┃        
 *          ┃   ┃
 *          ┃   ┃           
 *          ┃   ┗━━━┓
 *          ┃       ┣┓
 *          ┃       ┏┛
 *          ┗┓┓┏━┳┓┏┛
 *           ┃┫┫ ┃┫┫
 *           ┗┻┛ ┗┻┛
 */ 
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-12
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid (x+y>>1)
#define NM 20005
#define nm 100005
#define pi 3.1415926535897931
using namespace std;
const int inf=1000000007;
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}


struct edge{int t;edge*next;}e[nm],*h[NM],*o=e;
void add(int x,int y){o->t=y;o->next=h[x];h[x]=o++;}
int n,m,_x,_y,ans;
int d[NM],low[NM],suc[NM],cnt,tot,c[NM];
bool v[NM];
stack<int>s;


void tar(int x){
	d[x]=low[x]=++tot;s.push(x);
	link(x)if(!d[j->t]){tar(j->t);low[x]=min(low[x],low[j->t]);}
	else if(!suc[j->t])low[x]=min(low[x],low[j->t]);
	if(low[x]==d[x]){
		int t;cnt++;
		do{
			t=s.top();s.pop();
			suc[t]=cnt;c[cnt]++;
		}while(x!=t);
	}
}



int main(){
	while(~scanf("%d%d",&n,&m)){
		mem(d);mem(low);mem(suc);cnt=tot=0;mem(c);ans=0;
		mem(e);mem(h);o=e;mem(v);
		inc(i,1,m){_x=read();_y=read();add(_x+n,_y+n);}
		inc(i,n+1,n<<1)if(!d[i])tar(i);
		inc(i,n+1,n<<1)link(i)if(suc[i]!=suc[j->t])v[suc[i]]=true;
		inc(i,1,cnt)if(!v[i])if(ans)ans=-1;else ans=i;
		if(ans==-1)ans=0;
		printf("%d\n",c[ans]);
	}
	return 0;
}





Popular Cows
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 37944 Accepted: 15455

Description

Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10,000) cows, you are given up to M (1 <= M <= 50,000) ordered pairs of the form (A, B) that tell you that cow A thinks that cow B is popular. Since popularity is transitive, if A thinks B is popular and B thinks C is popular, then A will also think that C is
popular, even if this is not explicitly specified by an ordered pair in the input. Your task is to compute the number of cows that are considered popular by every other cow.

Input

* Line 1: Two space-separated integers, N and M

* Lines 2..1+M: Two space-separated numbers A and B, meaning that A thinks B is popular.

Output

* Line 1: A single integer that is the number of cows who are considered popular by every other cow.

Sample Input

3 3
1 2
2 1
2 3

Sample Output

1

Hint

Cow 3 is the only cow of high popularity.

Source

[Submit]   [Go Back]   [Status]   [Discuss]


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值