【拓扑排序topsort】——启动!!!

B3644 【模板】拓扑排序 / 家谱树

#include<bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define pb push_back
#define PII pair<int,int >
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
const int N = 1e6+5;
int n,m;
int in[N];
vector<int> v[N];
void topsort()
{
	vector<int> e;
	queue<int> q;
	for(int i=1;i<=n;i++) if(in[i]==0) q.push(i);
	while(q.size())
	{
		auto now = q.front();
		e.pb(now);
		q.pop();
		for(auto spot : v[now])
		{
			if(--in[spot]==0) q.push(spot);
		}
	}
	for(auto t:e) cout<<t<<" ";
}
signed main()
{
	IOS;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		int x;
		while(cin>>x&&x!=0)
		{
			v[i].pb(x);
			in[x]++;
		}
	}
	topsort();
}

P2712 摄像头

#include<bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define pb push_back
#define PII pair<int,int >
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
const int N = 1e6+5;
int n,m;
vector<int> v[N];
set<int> s;
int in[N];
int cnt=0;
void topsort()
{
	queue<int> q;
	for(int i=1;i<=510;i++) 
	if(in[i]==0&&s.count(i)) 
	{
		q.push(i);//没有摄像头看着并且该位置有摄像头
	} 
	while(q.size())
	{
		auto now = q.front();
		cnt++;
		q.pop();
		for(auto spot:v[now])
		{
			if(--in[spot]==0&&s.count(spot))//这个摄像头照着的地方可能没有摄像头 
			{
				q.push(spot);
			} 
		}
	}
}
signed main()
{
	IOS;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		int x,m;
		cin>>x>>m;
		s.insert(x);
		while(m--)
		{
			int y;
			cin>>y;
			v[x].pb(y);
			in[y]++;
		}
	}
	topsort();	
	if(cnt!=n) cout<<n-cnt;
	else cout<<"YES";	
}

P4017 最大食物链计数

#include<bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define pb push_back
#define PII pair<int,int >
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
const int N = 1e6+5;
int n,m;
int in[N];
int out[N];
int mod=80112002;
vector<int> v[N];
int dp[N];//表示有多少条不同的路径:生产者到i
void topsort()
{
	queue<int> q;
	for(int i=1;i<=n;i++)
	{
		if(in[i]==0)
		{
			q.push(i);
			dp[i] = 1;	
		}	
	}
	while(q.size())
	{
		int now = q.front();
		q.pop();
		for(auto spot:v[now])
		{
			dp[spot] = (dp[spot]+dp[now])%mod;
			if(--in[spot]==0) q.push(spot);
		}
	}
}
signed main()
{
	IOS;
	cin>>n>>m;
	for(int i=1;i<=m;i++)
	{
		int a,b;
		cin>>a>>b;
		v[a].pb(b);// a被b吃
		in[b]++;
		out[a]++;
	}
	topsort();
	int sum=0;
	for(int i=1;i<=n;i++)
	{
		if(out[i]==0) sum = (sum+dp[i])%mod;
	}
	cout<<sum;
}

P6145 [USACO20FEB] Timeline G

#include<bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define pb push_back
#define PII pair<int,int >
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
const int N = 1e6+5;
int n,m,k;
int in[N];
int va[N];
vector<PII> v[N];
int dp[N];//表示第i次挤奶最早为第几天
void topsort()
{
	queue<int> q;
	for(int i=1;i<=n;i++)
	{
		if(in[i]==0)
		{
			q.push(i);	
		}	
	}
	for(int i=1;i<=n;i++) dp[i] = va[i];//第i次挤奶最早是第S[i]天; 
	while(q.size())
	{
		int now = q.front();
		q.pop();
		for(auto t:v[now])
		{
			int spot = t.fi,w = t.se;
			//dis[a] = 4,dis[b] = 5
			//a -> spot至少3天 b-> spot至少5天
			//那么dis[spot] = 10
			//因为要求最早的日期,那么第几天要尽可能大 
			dp[spot] = max(dp[spot],dp[now]+w); 
			if(--in[spot]==0) q.push(spot);
		}
	}
}
signed main()
{
	IOS;
	cin>>n>>m>>k;
	for(int i=1;i<=n;i++) cin>>va[i];
	while(k--)
	{
		int a,b,c;
		cin>>a>>b>>c;
		v[a].pb({b,c});
		in[b]++;
	}
	topsort();
	for(int i=1;i<=n;i++) cout<<dp[i]<<"\n";
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值