Sunday, July 27, 2014

timus 1080 - Map coloring solution

timus 1080 - Map coloring statement: http://acm.timus.ru/problem.aspx?space=1&num=1080

timus 1080 - Map coloring solution: http://ideone.com/vNEv5A

//http://ideone.com/vNEv5A
//169445
#include <iostream>
#include <cstdio>
using namespace std;

int n, adj[105][105]={0}, c[105], a;

bool dfs(int v, int col) {
    if(c[v]!=-1 && c[v]==col) return true;
    else if(c[v]!=-1 && c[v]!=col) return false;
    c[v]=col;
    for(int i=0; i<n; i++) if(adj[v][i] && !dfs(i, 1-col)) return false;
    return true;
}

int main() {
    scanf("%d", &n);
    for(int i=0; i<n; i++) while(true) {
        scanf("%d", &a);
        if(a==0) break;
        adj[i][a-1]=adj[a-1][i]=1;
    }
    for(int i=0; i<105; i++) c[i]=-1;
    if(dfs(0, 0)==true) for(int i=0; i<n; i++) printf("%d", c[i]);
    else printf("-1");
    return 0;
}

note: the code is almost the same of irancoldfusion's: https://bitbucket.org/pykello/timus/src/a3037a3bd7a7/1080.cpp

No comments:

Post a Comment