POJ-2186-Popular Cows(Tarjan求强连通分量+缩点)

Popular Cows

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 43129 Accepted: 17543

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

题解:强联通分量缩点

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<queue>
#include<vector>
#include<cmath>
#define N 10005
#define M 50005
using namespace std;
int n,m,c,index,tot,sum,k,ans;
int head[N],low[N],dfn[N],belong[N],out[N];
bool vis[N];
stack<int>s;
struct ljh
{
	int to,next;
}e[M];
inline void init()
{
	k=0;
	c=0;
	index=0;
	tot=0;
	ans=0;
	memset(head,-1,sizeof(head));
	memset(dfn,0,sizeof(dfn));
	memset(low,0,sizeof(low));
	memset(belong,0,sizeof(belong));
	memset(vis,0,sizeof(vis));
	memset(out,0,sizeof(out));
}
inline void add(int x,int y)
{
	e[c].next=head[x];
	e[c].to=y;
	head[x]=c++;
}
void Tarjan(int x)
{
	low[x]=dfn[x]=++tot;
	s.push(x);
	vis[x]=1;
	for(int i=head[x];i!=-1;i=e[i].next)
	{
		int v=e[i].to;
		if(!low[v])
		{
			Tarjan(v);
			low[x]=min(low[x],low[v]);
		}
		else if(vis[v]==1)low[x]=min(low[x],dfn[v]);
	}
	if(low[x]==dfn[x])
	{
		int nex;
		index++;
		do
		{
			nex=s.top();
			s.pop();
			vis[nex]=0;
			belong[nex]=index;
		}while(!s.empty()&&nex!=x);
	}
	return;
}
int main()
{
	while(~scanf("%d%d",&n,&m))
	{
		init();
		for(int i=1;i<=m;i++)
		{
			int x,y;
			scanf("%d%d",&x,&y);
			add(x,y);
		}
		for(int i=1;i<=n;i++)
			if(!dfn[i])
			{
				Tarjan(i);
			}
		for(int i=1;i<=n;i++)
			for(int j=head[i];j!=-1;j=e[j].next)
				if(belong[i]!=belong[e[j].to])
				{
					out[belong[i]]++;
				}
		for(int i=1;i<=index;i++)
			if(!out[i])
			{
				k++;
				sum=i;
			}
		if(k==1)
		{
			for(int i=1;i<=n;i++)
				if(belong[i]==sum)
					ans++;
			printf("%d\n",ans);
		}
		else printf("0\n");
	}
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
POJ - 3616是一个题目,题目描述如下: 给定一组区间,每个区间有一个权重,要选择一些区间,使得这些区间的右端点都小于等于k,并且权重之和最大。请问最大的权重和是多少? 解决这个问题的思路是使用动态规划。首先,将区间按照左端点从小到大进行排序。然后,定义一个dp数组,dp[i]表示右端点小于等于i的所有区间所能得到的最大权重。 接下来,遍历每一个区间,对于每个区间i,将dp[i]初始化为区间i的权重。然后,再遍历i之前的每个区间j,如果区间j的右端点小于等于k,并且区间j的权重加上区间i的权重大于dp[i],则更新dp[i]为dp[j]加上区间i的权重。 最后,遍历整个dp数组,找到最大的权重和,即为所的答案。 下面是具体的代码实现: ```cpp #include <cstdio> #include <cstring> #include <algorithm> using namespace std; struct interval{ int start, end, weight; }; interval intervals[10005]; int dp[10005]; int n, m, k; bool compare(interval a, interval b) { if (a.start == b.start) { return a.end < b.end; } else { return a.start < b.start; } } int main() { while(~scanf("%d %d %d", &n, &m, &k)) { memset(dp, 0, sizeof dp); for (int i = 0; i < m; i++) { scanf("%d %d %d", &intervals[i].start, &intervals[i].end, &intervals[i].weight); } sort(intervals, intervals + m, compare); for (int i = 0; i < m; i++) { dp[i] = intervals[i].weight; for (int j = 0; j < i; j++) { if (intervals[j].end <= k && dp[j] + intervals[i].weight > dp[i]) { dp[i] = dp[j] + intervals[i].weight; } } } int maxWeight = 0; for (int i = 0; i < m; i++) { maxWeight = max(maxWeight, dp[i]); } printf("%d\n", maxWeight); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值