cf Ping-Pong (Easy Version)

http://codeforces.com/contest/320/problem/B

这道题看了很长时间没看懂, 就是个dfs;

 1 #include <cstdio>

 2 #include <cstring>

 3 #define maxn 2000

 4 using namespace std;

 5 

 6 int a[maxn],b[maxn];

 7 bool vis[maxn];

 8 int cnt=1;

 9 

10 bool dfs(int x,int y)

11 {

12     vis[x]=true;

13     if(x==y) return 1;

14     for(int i=1; i<=cnt; i++)

15     {

16         if(!vis[i]&&((a[i]<a[x]&&a[x]<b[i])||(a[i]<b[x]&&b[x]<b[i])))

17         {

18             if(dfs(i,y)) return true;

19         }

20     }

21     return false;

22 }

23 

24 int main()

25 {

26     int n,x,y,c;

27     scanf("%d",&n);

28     memset(vis,false,sizeof(vis));

29     for(int i=1; i<=n; i++)

30     {

31         scanf("%d%d%d",&c,&x,&y);

32         if(c==1)

33         {

34             a[cnt]=x; b[cnt++]=y;

35         }

36         else

37         {

38             memset(vis,false,sizeof(vis));

39             if(!dfs(x,y)) printf("NO\n");

40             else printf("YES\n");

41         }

42     }

43     return 0;

44 }
View Code

 

你可能感兴趣的:(version)