poj2513
제목: 나무 막대기 한 무더기의 좌우 양쪽 끝에 색을 칠하여 같은 색깔로 연결할 수 있습니다. 모든 나무 막대기를 연결할 수 있는지 묻습니다. EOF 마감
코드:
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
using namespace std;
struct node{
char str[20];
}s[550005],stemp[550005];
char a[20],b[20],temp[550005][20];
int fa[550005],sum[550005];
int cmp(struct node a,struct node b){
return strcmp(a.str,b.str)<0;
}
int binsearch(char *s,int n){
int low,high,mid;
low=0;high=n-1;
while(low<=high){
mid=(low+high)/2;
if(strcmp(temp[mid],s)==0)
return mid;
else if(strcmp(temp[mid],s)>0)
high=mid-1;
else if(strcmp(temp[mid],s)<0)
low=mid+1;
}
} //
int found(int x){
if(x!=fa[x])
fa[x]=found(fa[x]);
return fa[x];
}
int main(){ //
int i,j,k,p,xx,yy,flag; //
i=k=0; //
while(scanf("%s%s",a,b)!=EOF){
strcpy(s[i++].str,a);
strcpy(stemp[i-1].str,a);
strcpy(s[i++].str,b);
strcpy(stemp[i-1].str,b);
k++;
}
sort(s,s+i,cmp);
strcpy(temp[0],s[0].str);
p=1;
for(j=1;j<i;j++)
if(strcmp(s[j].str,s[j-1].str)!=0)
strcpy(temp[p++],s[j].str);
// for(j=0;j<p;j++)
// cout<<temp[j]<<endl;
for(j=0;j<p;j++)
fa[j]=j;
for(j=0;j<i;j+=2){
xx=binsearch(stemp[j].str,p);
yy=binsearch(stemp[j+1].str,p);
// cout<<stemp[j].str<<" "<<stemp[j+1].str<<endl;
// cout<<xx<<" "<<yy<<endl;
sum[xx]++;sum[yy]++;
if(found(fa[xx])!=found(fa[yy])) //
fa[found(fa[xx])]=found(fa[yy]);
}
flag=0;
for(i=0;i<p;i++)
if(fa[i]==i)
flag++;
if(flag>1){ //
printf("Impossible
");
return 0;
}
flag=0;
for(i=0;i<p;i++)
if(sum[i]%2!=0)
flag++;
if(flag==2||flag==0)
printf("Possible
");
else
printf("Impossible
");
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.