D. Toss a Coin to Your Graph...(二分)

Posted thusloop

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了D. Toss a Coin to Your Graph...(二分)相关的知识,希望对你有一定的参考价值。

D. Toss a Coin to Your Graph
二分答案 x
权值比小于等于x的点重新建图,之后拓扑排序,判断最长链是否大于等于k或者是否存在环

//#pragma GCC optimize(2)
//#pragma GCC optimize(3,"Ofast","inline")
#include<bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define pb push_back
#define pii pair<int,int>
#define yes cout<<"YES\\n"
#define no cout<<"NO\\n"
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const int inf=8e18;
const int maxn=2e5+100;
vector<int>g[maxn];
int a[maxn],in[maxn],vis[maxn],in2[maxn];
int n,m,k;
struct node

	int x,y;
t[maxn];

bool ck(int x)

	for(int i=1;i<=n;i++) 
	
		in2[i]=0;
		g[i].clear();
	
	for(int i=1;i<=m;i++)
	
		auto [u,v]=t[i];
		if(a[u]<=x&&a[v]<=x)
		
			g[u].pb(v);
			in2[v]++;
		
	
	queue<pii>q;
	for(int i=1; i<=n; i++)
	
		vis[i]=0;
		in[i]=in2[i];
		if(in[i]==0&&a[i]<=x)
		
			q.push(i,1);
		
	
	int mx=0;
	while(!q.empty())
	
		auto now=q.front();
		vis[now.fi]=1;
		mx=max(mx,now.se);
		q.pop();
		for(auto it:g[now.fi])
		
			in[it]--;
			if(in[it]==0&&a[it]<=x) 
			q.push(it,now.se+1);
		
	
	bool fg=0;
	for(int i=1; i<=n; i++)
	
		if(!vis[i]&&a[i]<=x&&in[i]!=0) fg=1;
	
	if(mx>=k) fg=1;
	return fg;

signed main()

	IOS
	cin>>n>>m>>k;
	for(int i=1; i<=n; i++)
	
		cin>>a[i];
	
	for(int i=1; i<=m; i++)
	
		int x,y;
		cin>>x>>y;
		t[i]=x,y;
		g[x].pb(y);
		in[y]++;
	
	for(int i=1;i<=n;i++)
	in2[i]=in[i];
	int l=0,r=1e9;
	//cout<<ck(4);
	while(l<=r)
	
		int mid=(l+r)>>1;
		if(ck(mid)) r=mid-1;
		else l=mid+1;
	
	if(l==(int)1e9+1) l=-1;
	cout<<l<<"\\n";

/*
6 7 100
1 10 2 3 4 5
1 2
1 3
3 4
4 5
5 6
6 2
2 5

*/

以上是关于D. Toss a Coin to Your Graph...(二分)的主要内容,如果未能解决你的问题,请参考以下文章

D. Toss a Coin to Your Graph...(二分)

POJ 3440 Coin Toss(概率公式)

poj3440--Coin Toss(几何上的概率)

UVa 10328 Coin Toss(Java大数+递推)

made his acquaintance|adequate|advisable|announce|contrived to|made up|toss|considering that

[Coding Made Simple] Coin Changes Number of ways to get a total