Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

谁能帮我看看,这道题就是判欧拉路径和连通吧,还有其他tricks?为什么老是wa

Posted by tianya at 2005-11-07 10:53:47 on Problem 1386
#include<stdio.h>
#include<memory.h>
#include<stdlib.h>
#include<string.h>


#ifdef debug
#define input file
FILE*file=fopen("tmp.txt","r");
#else
#define input stdin
#endif

#define SIZE 100000
#define WORD 1000

int in[26];             //入度出度判欧拉路径
int out[26];
int visited[26];
int uset[26];           //并查集判联通
int T,N;

int root(int i){
    if(uset[i]==i)return i;
    uset[i]=root(uset[i]);
    return uset[i];
}

void combine(int i,int j){
    uset[uset[j]]=uset[i];
}        
            
    

int main(){
    fscanf(input,"%d",&T);
    while(T--){
        fscanf(input,"%d",&N);
        int i,j;
        char s[WORD+1];
        memset(in,0,sizeof(in));
        memset(out,0,sizeof(out));
        memset(visited,0,sizeof(visited));
        for(i=0;i<26;i++){
            uset[i]=i;
        }    
        for(i=0;i<N;i++){
            fscanf(input,"%s",s);
            int first,last;
            first=s[0]-'a';
            last=s[strlen(s)-1]-'a';
            
            in[first]++;
            out[last]++;
            visited[first]=1;
            visited[last]=1;
            if(root(first)!=root(last))combine(first,last);
        }
        int num_in=0,num_out=0;
        for(i=0;i<26;i++){                 //判欧拉路径
            if(!visited[i])continue;
            if(in[i]!=out[i]){
                if(in[i]==(out[i]-1))num_in++;
                else if(in[i]==(out[i]+1))num_out++;
                else break;
            }
        }
        if(i<26||(num_in!=1)||(num_out!=1)){
            printf("The door cannot be opened.\n");
            continue;
        }
        for(i=0;i<26;i++){          //判联通
            if(visited[i])break;
        }
        int _root=root(i);
        for(i++;i<26;i++){
            if(visited[i]&&root(i)!=_root)break;
        }
        if(i<26){
            printf("The door cannot be opened.\n");
            continue;
        }
        printf("Ordering is possible.\n");
    }
    system("pause");
    return 0;
}                          
        

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator