CodeForces-1217D (topological sorting / dfs judgment ring)

The meaning of problems

https://vjudge.net/problem/CodeForces-1217D

Please give a directed graph coloring, so that none of the rings is only one color, you need to minimize the number of colors used.

Thinking

Because it is a directed graph, each ring two colors to meet up. Therefore, a maximum of 2, a minimum of 1.

Act 1 dfs:

Using dfs Analyzing directed acyclic graph, each ring constituting the last piece of colored edges 2, a remaining dyed.

2 topological sorting method:

Easy to find, for a directed graph, if the number of points then the ring will not monotonic, because that last point will connect back to the starting point.

So we put u <v dyed 1, u> v dyed 2, then topological sorting sentenced ring, ring if there is then output staining protocol, otherwise full output 1.

Code

Method 1:

#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
const int N=5e3+5;
const int mod=1e9+7;
const double eps=1e-8;
const double PI = acos(-1.0);
#define lowbit(x) (x&(-x))
vector<int> g[N];
int e[N][N],vis[N],col[N],gg,in[N];
void dfs(int u)
{
    int sz=g[u].size();
    in [u] = 1;
    for(int i=0;i<sz;i++)
    {
        int v=g[u][i];
        if(!vis[v])
        {
            to [v] = 1;
            col[e[u][v]]=1;
            dfs(v);
        }
        else if(in[v])
        {
            col[e[u][v]]=2;
            gg=1;
        }
        else
        {
            col[e[u][v]]=1;
        }
    }
    in [u] = 0;
}
int main ()
{
    std::ios::sync_with_stdio(false);
    int n,m;
    while(cin>>n>>m)
    {
        memset(vis,0,sizeof(vis));
        for(int i=1;i<=m;i++)
        {
            you and, v;
            cin>>u>>v;
            g[u].push_back(v);
            e[u][v]=i;
        }
        gg=0;
        for(int i=1;i<=n;i++)
        {
            if(!vis[i])
            {
             //   col[i]=1;
                view [i] = 1;
                dfs(i);
            }
        }
        if(!gg)
        {
            cout<<1<<endl;
            for(int i=1;i<=m;i++)
                cout<<1<<" ";
            cout<<endl;
        }
        else
        {
            cout<<2<<endl;
            for(int i=1;i<=m;i++)
                cout<<col[i]<<" ";
            cout<<endl;
        }
    }
    return 0;
}

  

Method 2:

#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
const int N=200005;
const int mod=1e9+7;
const double eps=1e-8;
const double PI = acos(-1.0);
#define lowbit(x) (x&(-x))
vector<int> g[N];
int col [N] of [N];
int n,m;
bool mouse ()
{
    queue<int> q;
    for(int i=1; i<=n; i++)
        if(du[i]==0) q.push(i);
    int cnt=0;
    while(!q.empty())
    {
        int t=q.front();
        for(int i:g[t])
        {
            you [in] -;
            if (to [i] == 0)
                q.push(i);
        }
        cnt++;
        q.pop();
    }
    if(cnt!=n)
        return false;
    return true;
}
int main ()
{
    std::ios::sync_with_stdio(false);
    cin>>n>>m;
    for(int i=1; i<=m; i++)
    {
        you and, v;
        cin>>u>>v;
        g[u].push_back(v);
        col[i]=(u<v);
        you [v] ++;
    }
    if(topo())
    {
        cout<<1<<endl;
        for(int i=1;i<=m;i++)
            cout<<1<<" ";
        cout<<endl;
    }
    else
    {
        cout<<2<<endl;
        for(int i=1;i<=m;i++)
            cout<<col[i]+1<<" ";
        cout<<endl;
    }
    return 0;
}

  

Guess you like

Origin www.cnblogs.com/mcq1999/p/11983585.html