Codeforces Round #656 (Div. 3) E (topological sort)

Title

Give you a graph, there are undirected edges and directed edges, ask you to change the undirected edges into directed edges, and ask if there is a kind of change. Yes, the graph has no loops. If yes, and output this change Scheme, if there is no output no.

answer

In fact, I had some ideas when playing games, but there was no time. Just topsort directly. When sorting, if you encounter an undirected edge, just add a direction to it. See the code for details.

#include <bits/stdc++.h>
#define ll long long
#define pi pair<int,int>
#define mk make_pair
#define pb push_back
using namespace std;
 
const int maxn = 2e5+10;
vector<int>G[maxn];
struct node{
    
    
	int op,u,v;
}a[maxn];
 
int in[maxn];
map<pi,int>mp,vis;
 
bool topsort(int n)
{
    
    
	queue<int>q;
	for(int i=1;i<=n;i++)if(in[i] == 0)q.push(i);
	while(q.size())
	{
    
    
		int u = q.front();
		q.pop();
		for(int i=0;i<G[u].size();i++)
		{
    
    
			int v = G[u][i];
			if(vis[mk(v,u)] == 2)continue;
			if(mp[mk(u,v)]) {
    
    
				vis[mk(u,v)] = 2;
				continue;
			}
			if((--in[v]) == 0)q.push(v);
		}
	}
	for(int i=1;i<=n;i++)if(in[i])return false;
	return true;
}
 
int main()
{
    
    
	int T;
	cin >> T;
	while(T--)
	{
    
    
		int n,m;
		scanf("%d%d",&n,&m);
		mp.clear();
		vis.clear();
		for(int i=1;i<=n;i++)in[i] = 0, G[i].clear();
		for(int i=1;i<=m;i++)
		{
    
    
			scanf("%d%d%d",&a[i].op,&a[i].u,&a[i].v);	
			if(a[i].op == 1)
			{
    
    
				in[a[i].v]++;
				G[a[i].u].pb(a[i].v);
			}
			else
			{
    
    
				mp[mk(a[i].u , a[i].v)] = 1;
				mp[mk(a[i].v , a[i].u)] = 1;
				G[a[i].u].pb(a[i].v);
				G[a[i].v].pb(a[i].u);
			}
		}
		
		bool ok = topsort(n);
		if(ok)
		{
    
    
			puts("YES");
			for(int i=1;i<=m;i++)
			{
    
    
				if(a[i].op == 1)printf("%d %d\n",a[i].u,a[i].v);
				else 
				{
    
    
					if(vis[mk(a[i].u,a[i].v)] == 2)printf("%d %d\n",a[i].u,a[i].v);
					else if(vis[mk(a[i].v,a[i].u)] == 2)printf("%d %d\n",a[i].v,a[i].u);
				}
			}
		}
		else puts("NO");
	} 
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_44499508/article/details/107450251
Recommended