UVa 10004 - Bicoloring


10004-Bicoloring
32340
 
42.67%
8939
 
86.93%
제목 링크:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=105
 
제목 유형:검색
 
제목:
In 1976 the ``Four Color Map Theorem" was proven with the assistance of a computer. This theorem states that every map can be colored using only four colors, in such a way that no region is colored using the same color as a neighbor region.
Here you are asked to solve a simpler similar problem. You have to decide whether a given arbitrary connected graph can be bicolored. That is, if one can assign colors (from a palette of two) to the nodes in such a way that no two adjacent nodes have the same color. To simplify the problem you can assume:
 
  • no node will have an edge to itself.
  • the graph is nondirected. That is, if a nodeais said to be connected to a nodeb, then you must assume thatbis connected toa.
  • the graph will be strongly connected. That is, there will be at least one path from any node to any other node.

  •  
    제목 번역:
    1976 년 에'사색 정리'는 컴퓨터 의 도움 으로 증명 되 었 다.이 정 리 는 모든 지 도 를 네 가지 색 으로 만 채 울 수 있 고 인접 지역 의 색 이 같 지 않다 는 것 을 알 린 다.
    이제 더 쉬 운 문 제 를 해결 하 게 해 줄 게.너 는 주어진 임의의 연 결 된 그림 을 두 가지 색 으로 채 울 수 있 는 지 를 결정 해 야 한다.그 중 하 나 를 한 가지 색 으로 나 누 면 직접 연 결 된 두 노드 가 같은 색 이 되 지 않도록 해 야 한 다 는 것 이다.문 제 를 더욱 간단하게 하기 위해 서 너 는 가정 할 수 있다.
    1.노드 가 없 으 면 자신 에 게 연 결 됩 니 다.
    2.무방 향도 다.즉,a 가 b 를 연결 하면 b 도 a 를 연결 합 니 다.
    3.그림 은 강 한 연결 이다.그 러 니까 최소한 한 가지 경로 가 모든 노드 로 갈 수 있다 는 거 야.
     
    샘플 입력:
     
    3
    3
    0 1
    1 2
    2 0
    9
    8
    0 1
    0 2
    0 3
    0 4
    0 5
    0 6
    0 7
    0 8
    0

     
     
     
    샘플 출력:
     
    NOT BICOLORABLE.
    BICOLORABLE.
    

     
     
    분석 과 총 결:
    방법 1:BFS 검색
    제목 에서 알 수 있 듯 이 모든 결점 에 대해 서 는 그것 과 연 결 된 모든 점 이 이 점 과 색깔 이 달라 야 한다.그러면 자 연 스 럽 게 광 검색 으로 할 수 있 습 니 다.그 중의 하 나 를 선택 하여 이 점 에 값 을 부여 할 수 있 습 니 다.숫자 0 으로 대체 한 다음 에 광 검색 을 할 수 있 습 니 다.그러면 그 와 인접 한 모든 점 은 다른 색 으로 할당 할 수 있 고 1 로 대체 할 수 있 습 니 다.이렇게 찾 아 보면 한 가지 점 이 이미 할당 되 었 다 면 그 가 이미 가지 고 있 는 가치 가 이번에 주 려 는 가치 와 같 는 지,같은 것 이 라면 계속 하 라.다 르 면 안 된다 고 직접 판단 한다.
     
    BFS 코드:
     
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #define MAXN 210
    using namespace std;
    int n, m, a, b, G[MAXN][MAXN], lastPos;
    int vis[210],edge[250][2];
    bool flag;
    
    int que[100000];
    void bfs(int pos){
        int front=0, rear=1;
        que[0] = pos;
        while(front < rear){
            int m = que[front++];
            for(int i=0; i<n; ++i){
                if(G[m][i]){ 
                    if(!vis[i]){
                        que[rear++] = i;
                        vis[i] = vis[m]+1;
                    }
                    else if(vis[i]==vis[m]){
                        flag = true;
                        return;
                    }
                }
            }
        }
    }
    int main(){
    #ifdef LOCAL
        freopen("input.txt","r",stdin);
    #endif
        while(~scanf("%d",&n) && n){
            memset(G, 0,sizeof(G));
            scanf("%d",&m);
            for(int i=0; i<m; ++i){
                scanf("%d %d",&a,&b);
                G[a][b] = 1;
                G[b][a] = 1;
            }
            memset(vis, 0, sizeof(vis));
            vis[0] = 1;
            flag = false;
            bfs(0);
            
            if(flag) printf("NOT BICOLORABLE.
    "); else printf("BICOLORABLE.
    "); } return 0; }

     
     
     
    방법 2:DFS 심층 검색
    마찬가지 로 이 문 제 는 심층 검색 으로 도 풀 수 있다.깊 은 검색 의 기본 사상 은 한 방향 을 따라 계속 검색 하고 한 걸음 도 걷 지 않 고 염색 하 는 것 이다.현재 이 점 의 색 은 위의 색 과 반대 된다.만약 염 색 된 색(즉 고리 가 있 음)을 찾 았 다 면,이미 있 는 색 이 이번에 준 색 과 일치 하 는 지 아 닌 지 를 판단 해 보 자.일치 하지 않 으 면 안 된다 고 판단 합 니 다.
     
    DFS 코드:
     
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #define MAXN 210
    using namespace std;
    int n, m, a, b, G[MAXN][MAXN], lastPos;
    int vis[210];
    bool flag;
    
    void dfs(int pos){
        if(flag) return;
        for(int i=0; i<n; ++i){
            if(G[pos][i]){
                if(vis[i]==-1){
                    vis[i] = !vis[pos];
                    dfs(i);
                    vis[i] = -1;
                }
                else if(vis[i] != !vis[pos]){
                    flag = true;
                    return;
                }
            }
        }
    }
    
    int main(){
    #ifdef LOCAL
        freopen("input.txt","r",stdin);
    #endif
        while(~scanf("%d",&n) && n){
            memset(G, 0,sizeof(G));
            scanf("%d",&m);
            for(int i=0; i<m; ++i){
                scanf("%d %d",&a,&b);
                G[a][b] = 1;
                G[b][a] = 1;
            }
            flag = false;
            
            memset(vis, -1, sizeof(vis));
            vis[0] = 0;
            dfs(0);
    
            if(flag) printf("NOT BICOLORABLE.
    "); else printf("BICOLORABLE.
    "); } return 0; }

     
     
    -생명의 의 미 는 그 에 게 의 미 를 부여 하 는 데 있다.
     
    오리지널
    http://blog.csdn.net/shuangde800
    ,By D_Double
     
     
     
     
     
     
     
     

    좋은 웹페이지 즐겨찾기