| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
WA,求BT测试数据?#include <iostream>
using namespace std;
int n,slen;
bool adj[26][26],m[26][26];
bool cha[26],ha[26];
int r;
int seq[27];
int topo(){
bool goon=true,unq=true;
int i,j,k,d,a1;
r=0;
for(i=0;i<26;i++){
ha[i]=cha[i];
for(j=0;j<26;j++)
m[i][j] = adj[i][j];
}
while(goon){
a1=-1;
goon=false;
for(i=0;i<26;i++){
d=0;
if(ha[i]){
goon=true;
for(j=0;j<26;j++) {
if(!ha[j]||i==j) continue;
if(m[j][i]) d++;
}
if(d==0) {
if(a1<0) a1=i;
else unq=false;
}
}
}
if(a1<0&&goon) return 2; //有环
seq[r++]=a1+65;
ha[a1]=false;
if(a1>=0&&a1<=25)
for(k=0;k<26;k++) m[a1][k]=false;
}
if(unq) return 1; //唯一的排序
else return 0; //不唯一的排序
}
int main(){
int i,ch1,ch2,q,val;
bool inc,fin;
while(scanf("%d %d",&n,&slen),n){
inc=true;
fin=false;
memset(cha,0,sizeof(cha));
memset(adj,0,sizeof(adj));
for(i=0;i<slen;i++){
if(fin){
getchar();getchar();getchar();getchar();
continue;
}
getchar();
ch1=getchar();
getchar();
ch2=getchar();
if(ch1==ch2) {
inc=false;
printf("Inconsistency found after %d relations.\n",i+1);
fin=true;
continue;
}
adj[ch1-65][ch2-65]=true;
cha[ch1-65]=true;
cha[ch2-65]=true;
val=topo();
if(val==2){
inc=false;
printf("Inconsistency found after %d relations.\n",i+1);
fin=true;
continue;
}else if(val==1&&r==n+1){
printf("Sorted sequence determined after %d relations: ",i+1);
for(q=0;q<n;q++){
printf("%c",seq[q]);
}
printf(".\n");
fin=true;
continue;
}
}
if(r<=n&&inc) printf("Sorted sequence cannot be determined.\n");
}
return 1;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator