求助大佬这道题最后两个点卡进死循环了(求差错

P2731 [USACO3.3] 骑马修栅栏 Riding the Fences

Ning_Mew @ 2018-03-25 21:17:00

//It is coded by Ning_Mew on 3.23
#include<bits/stdc++.h>
using namespace std;

const int maxn=1500+7;

int n;
int root[510],a[510][510];

bool check(){
  for(int i=1;i<=500;i++){
    if(root[i]>0){
      //cout<<"WA:"<<i<<' '<<root[i]<<endl;
      return true;
    }
  }return false;
}
int main(){
  scanf("%d",&n);
  memset(a,0,sizeof(a));
  memset(root,0,sizeof(root));
  for(int i=1;i<=n;i++){
    int x,y;scanf("%d%d",&x,&y);
    a[x][y]++;a[y][x]++;
    root[x]++;root[y]++;
  }
  int u=1,last=0;
  for(int i=1;i<=500;i++){
    if(root[i]%2==1){u=i;last=-1;break;}
  }
  if(last==-1){
    for(int i=1;i<=500;i++){
      if(root[i]%2==1&&i!=u){last=i;break;}
    }
  }
  else last=u;
  //for(int i=1;i<=5;i++)cout<<root[i]<<' ';cout<<endl; 
  printf("%d\n",u);
  int T=8;
  //while(T--){
  while(check()){
    int v=501;
    //cout<<"root[1]:"<<root[1]<<endl;
    for(int i=1;i<=500;i++){
      if(a[u][i]==0)continue;
      if(i!=last||(root[last]!=1&&i==last)){v=min(v,i);break;} 
    }
    //cout<<v<<' '<<box<<endl;
    if(v==501)v=last;
    root[u]--;root[v]--;
    a[u][v]--;a[v][u]--; 
    printf("%d\n",v);u=v;
  } 
  return 0;
} 

by Ning_Mew @ 2018-03-25 21:50:09

大概知道问题在哪了


|