biduw 发表于 2015-11-24 14:14:21

UVa 10763

Problem E

Foreign Exchange

Input: standard input

Output: standard output

Time Limit: 1 second




  Your non-profit organization (iCORE - international
Confederation of Revolver Enthusiasts) coordinates a very successful foreign student exchange program. Over the last few years, demand has sky-rocketed and now you need assistance with your task.

  The program your organization runs works as follows: All candidates are asked for their original location and the location they would like to go to. The program works out only if every student has a suitable exchange partner. In other words, if a student
wants to go from A to B, there must be another student who wants to go from B to A. This was an easy task when there were only about 50 candidates, however now there are up to
500000 candidates!

Input
  The input file contains multiple cases. Each test case will consist of a line containing
n - the number of candidates (1≤n≤500000), followed by
n lines representing the exchange information for each candidate. Each of these lines will contain
2 integers, separated by a single space, representing the candidate's original location and the candidate's target location respectively. Locations will be represented by nonnegative integer numbers. You may assume that no candidate will have
his or her original location being the same as his or her target location as this would fall into the domestic exchange program. The input is terminated by a case where
n = 0; this case should not be processed.

Output
  For each test case, print "YES" on a single line if there is a way for the exchange program to work out, otherwise print
"NO".

Sample Input Output for Sample Input
  10
  1 2
  2 1
  3 4
  4 3
  100 200
  200 100
  57 2
  2 57
  1 2
  2 1
  10
  1 2
  3 4
  5 6
  7 8
  9 10
  11 12
  13 14
  15 16
  17 18
  19 20
  0


  YES
  NO


  Problem setter: Gilbert Lee, University of Alberta, Canada

这题目用哈希做刚刚好, 注意优化的是凡是匹配的点以后就可以不用匹配了,否则很容易就超时
#include <stdio.h>
#include <string.h>
#include <math.h>
struct num
{
char s1,s2;
int next,status,pos;
}a;
int b;
int prim=1000001;
struct Num
{
char s1,s2;
}c;
int status;
int main()
{
int i,j,n,m,s,t,tag,s1,s2,yushu,x,l,key;
while(scanf(&quot;%d&quot;,&n)!=EOF)
{
if(n==0)
{
break;
}
memset(b,-1,sizeof(b));
for(i=0,tag=0;i<=n-1;i++)
{
scanf(&quot;%s %s&quot;,c.s1,c.s2);
l=strlen(c.s1);
for(j=0,s1=0;j<=l-1;j++)
{
s1=(s1*10%prim+c.s1-'0')%prim;
}
l=strlen(c.s2);
for(j=0,s2=0;j<=l-1;j++)
{
s2=(s2*10%prim+c.s2-'0')%prim;
}
yushu=(s1*10%prim+s2%prim)%prim;
strcpy(a.s1,c.s1);
strcpy(a.s2,c.s2);
a.next=b; a.pos=i;
a.status=0;b=tag;
tag++;
}
memset(status,0,sizeof(status));
for(i=0;i<=n-1;i++)
{
if(!status)
{
l=strlen(c.s2);
for(j=0,s2=0;j<=l-1;j++)
{
s2=(s2*10%prim+c.s2-'0')%prim;
}
l=strlen(c.s1);
for(j=0,s1=0;j<=l-1;j++)
{
s1=(s1*10%prim+c.s1-'0')%prim;
}
yushu=(s2*10%prim+s1%prim)%prim;
for(x=b,key=0;x!=-1; x=a.next)
{
if(!a.status&&!status.pos])
{
if(strcmp(a.s1,c.s2)==0&&strcmp(a.s2,c.s1)==0)
{
key=1;
a.status=1;
status=1;
status.pos]=1;
break;
}
}
}
if(!key)
{
break;
}
}
}
if(i==n)
{
printf(&quot;YES\n&quot;);
}else
{
printf(&quot;NO\n&quot;);
}
}
return 0;
}
页: [1]
查看完整版本: UVa 10763