关闭

SGU 121 Bridges painting

523人阅读 评论(0) 收藏 举报

一次性ac!!只有一种情况不可以,那就是全部连成一个环,且环上点的个数为奇数!

#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<cmath>
#include<cassert>
#include<cstring>
#include<iomanip>
using namespace std;

#ifdef _WIN32
#define i64 __int64
#define out64 "%I64d\n"
#define in64 "%I64d"
#else
#define i64 long long
#define out64 "%lld\n"
#define in64 "%lld"
#endif

#define FOR(i,a,b)      for( int i = (a) ; i <= (b) ; i ++)
#define FF(i,a)         for( int i = 0 ; i < (a) ; i ++)
#define FFD(i,a)        for( int i = (a)-1 ; i >= 0 ; i --)
#define S64(a)          scanf(in64,&a)
#define SS(a)           scanf("%d",&a)
#define LL(a)           ((a)<<1)
#define RR(a)           (((a)<<1)+1)
#define SZ(a)           ((int)a.size())
#define PP(n,m,a)       puts("---");FF(i,n){FF(j,m)cout << a[i][j] << ' ';puts("");}
#define pb              push_back
#define CL(Q)           while(!Q.empty())Q.pop()
#define MM(name,what)   memset(name,what,sizeof(name))
#define read            freopen("in.txt","r",stdin)
#define write           freopen("out.txt","w",stdout)

const int inf = 0x3f3f3f3f;
const i64 inf64 = 0x3f3f3f3f3f3f3f3fLL;
const double oo = 10e9;
const double eps = 10e-10;
const double pi = acos(-1.0);

i64 gcd(i64 _a, i64 _b)
{
    if (!_a || !_b)
    {
        return max(_a, _b);
    }
    i64 _t;
    while (_t = _a % _b)
    {
        _a = _b;
        _b = _t;
    }
    return _b;
};

i64 ext_gcd (i64 _a, i64 _b, i64 &_x, i64 &_y)
{
    if (!_b)
    {
        _x = 1;
        _y = 0;
        return _a;
    }
    i64 _d = ext_gcd (_b, _a % _b, _x, _y);
    i64 _t = _x;
    _x = _y;
    _y = _t - _a / _b * _y;
    return _d;
}

i64 invmod (i64 _a, i64 _p)
{
    i64 _ans, _y;
    ext_gcd (_a, _p, _ans, _y);
    _ans < 0 ? _ans += _p : 0;
    return _ans;
}

const int maxn = 111;

int col[maxn][maxn];
int vis[maxn];
bool ok[maxn];

vector<int>g[maxn];
int n;
int dfn;

void dfs(int now)
{
    int to;
    for(int i=0;i<g[now].size();i++)
    {
        to = g[now][i];
        if(!vis[now])
        {
            col[now][to] = 1;
            col[to][now] = 1;
            vis[now] =1;
            vis[to] = 1;
            dfs(to);
        }
        else if(vis[now]==1)
        {
            if(col[now][to])
            {
                continue;
            }
            else
            {
                ok[now] = true;
                col[now][to] = 2;
                col[to][now] = 2;
                vis[to] = 2;
                dfs(to);
            }
        }
        else
        {
            if(vis[now]==2)
            {
                if(col[now][to])
                {
                    continue;
                }
                else
                {
                    ok[now] = true;
                    col[now][to] = 1;
                    col[to][now] = 1;
                    vis[to] = 1;
                    dfs(to);
                }
            }
        }
    }
}

int can()
{
    for(int i=1;i<=n;i++)
    {
        if(g[i].size()>=2)
        {
            return i;
        }
    }
    return -1;
}

bool judge()
{
    int now,to;
    for(int i=1;i<=n;i++)
    {
        if(!ok[i])
        {
            return false;
        }
    }

    return true;
}

void start()
{
    MM(col,0);
    MM(vis,0);
    MM(ok,false);
    int temp = can();
    if(temp==-1)
    {
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<g[i].size();j++)
            {
                cout<<"1 ";
            }
            cout<<"0"<<endl;
        }
        return ;
    }
    for(int i=1;i<=n;i++)
    {
        if(g[i].size()<=1)
        {
            ok[i]=true;
        }
    }
    for(int i=1;i<=n;i++)
    {
        if( !vis[i] && g[i].size()>=3)
        {
            dfs(i);
        }
    }
    for(int i=1;i<=n;i++)
    {
        if(!vis[i] && g[i].size()>=2)
        {
            dfs(i);
        }
    }
    for(int i=1;i<=n;i++)
    {
        if(!vis[i] )
        {
            dfs(i);
        }
    }
    if(!judge())
    {
        cout<<"No solution"<<endl;
        return ;
    }
    int now,to;
    for(int i=1;i<=n;i++)
    {
        now = i;
        FF(j,g[i].size())
        {
            to = g[i][j];
            cout<<col[now][to]<<" ";
        }
        cout<<"0"<<endl;
    }
    return ;
}

int main()
{
    while(cin>>n)
    {
        FF(i,maxn) g[i].clear();
        int now;
        for(int i=1;i<=n;i++)
        {
            while(true)
            {
                cin>>now;
                if(now)
                {
                    g[i].pb(now);
                }
                else
                {
                    break;
                }
            }
        }
        start();
    }
    return 0;
}




0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:330800次
    • 积分:6305
    • 等级:
    • 排名:第3876名
    • 原创:299篇
    • 转载:19篇
    • 译文:0篇
    • 评论:126条
    最新评论