D Coloring Edges (topological sorting/dfs judgment ring)

Topic link

Main idea:

Coloring the edges of a directed graph requires that the colors of the edges in a ring cannot be the same.

Ideas:

If the graph has no loops, then all outputs are 1. If there are loops, then the edge of the large node pointing to the small node is colored 2; otherwise, it is colored 1. There are only two coloring cases at most, because there will be no connection between one ring and the other, so just make sure that each ring is legal.
So now only the judgment ring is left, which can be topological sort or dfs. The discriminant cycle of dfs is different from that of undirected graph, which needs attention.
DFS:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll inf=0x3f3f3f3f3f3f3f3f;
const int N=5e3+10;
struct node
{
    
    
    int to,next;
}e[N];
int flag=0,head[N],cnt=0,a[N],b[N],n,m,vis[N];
void addedge(int u,int v)
{
    
    
    e[++cnt].to=v;
    e[cnt].next=head[u];
    head[u]=cnt;
}
void dfs(int now)
{
    
    
    vis[now]=-1;//表示正在访问
    for(int i=head[now];i;i=e[i].next)
    {
    
    
        int to=e[i].to;
        if(vis[to]==0) dfs(to);
        else if(vis[to]==-1)
        {
    
    
            flag=1;
            return;
        }
    }
    vis[now]=1;//该节点子节点全部访问完。
}
int main()
{
    
    
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)
    {
    
    
        scanf("%d%d",&a[i],&b[i]);
        addedge(a[i],b[i]);
    }
    for(int i=1;i<=n;i++)
    {
    
    
        if(vis[i]==0) dfs(i);
    }
    if(!flag)
    {
    
    
        printf("1\n");
        for(int i=1;i<=m;i++) printf("1 ");
        printf("\n");
    }
    else
    {
    
    
        printf("2\n");
        for(int i=1;i<=m;i++)
        {
    
    
            if(a[i]>b[i]) printf("2 ");
            else printf("1 ");
        }
        printf("\n");
    }
    return 0;
}

topsort:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll inf=0x3f3f3f3f3f3f3f3f;
const int N=5e3+10;
struct node
{
    
    
    int to,next;
}e[N];
int num=0,head[N],cnt=0,deg[N],t[N],a[N],b[N],n,m;
void addedge(int u,int v)
{
    
    
    e[++cnt].to=v;
    e[cnt].next=head[u];
    head[u]=cnt;
}
void topsort()
{
    
    
    queue<int>q;
    for(int i=1;i<=n;i++) if(deg[i]==0) q.push(i);
    while(!q.empty())
    {
    
    
        int now=q.front();q.pop();
        t[now]=++num;
        for(int i=head[now];i;i=e[i].next)
        {
    
    
            int to=e[i].to;
            deg[to]--;
            if(deg[to]==0) q.push(to);
        }
    }
}
int main()
{
    
    
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)
    {
    
    
        scanf("%d%d",&a[i],&b[i]);
        deg[b[i]]++;
        addedge(a[i],b[i]);
    }
    topsort();
    if(num==n)
    {
    
    
        printf("1\n");
        for(int i=1;i<=m;i++) printf("1 ");
        printf("\n");
    }
    else
    {
    
    
        printf("2\n");
        for(int i=1;i<=m;i++)
        {
    
    
            if(a[i]>b[i]) printf("2 ");
            else printf("1 ");
        }
        printf("\n");
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/amazingee/article/details/107565486