Last active
July 11, 2017 15:27
-
-
Save IvanIsCoding/094b6ce65611749ca7ae8f3ef8afc0ac to your computer and use it in GitHub Desktop.
Seletiva IOI 2013
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Ivan Carvalho | |
// Ilhas - Seletiva IOI - OBI 2013 | |
#include <bits/stdc++.h> | |
#define MAXN 1010 | |
int U[MAXN*MAXN],V[MAXN*MAXN],reservado[MAXN],pai[MAXN],peso[MAXN],pares[MAXN]; | |
int find(int x){ | |
if (x==pai[x]) return x; | |
return pai[x] = find(pai[x]); | |
} | |
void join(int x, int y){ | |
x = find(x); | |
y = find(y); | |
if (x==y) return; | |
if (peso[x]< peso[y]) pai[x] = y; | |
else if(peso[x] > peso[y]) pai[y] = x; | |
else { | |
pai[x] = y; | |
peso[y]++; | |
} | |
} | |
int main(){ | |
int n,b,p; | |
scanf("%d %d %d",&n,&b,&p); | |
for(int i=1;i<=n;i++) pai[i] = i; | |
for(int i=1;i<=b;i++) scanf("%d %d",&U[i],&V[i]); | |
for(int i=1;i<=p;i++){ | |
int davez; | |
scanf("%d",&davez); | |
reservado[davez] = 1; | |
} | |
int conjuntos = n - p; | |
for(int i=1;i<=b;i++){ | |
if (reservado[U[i]] && reservado[V[i]]) continue; | |
if (reservado[U[i]]){ | |
pares[U[i]]++; | |
continue; | |
} | |
if (reservado[V[i]]){ | |
pares[V[i]]++; | |
continue; | |
} | |
if (find(U[i]) != find(V[i])){ | |
join(U[i],V[i]); | |
conjuntos--; | |
} | |
} | |
if (conjuntos != 1){ | |
printf("N\n"); | |
return 0; | |
} | |
for(int i=1;i<=n;i++) if (reservado[i] && !pares[i]){ | |
printf("N\n"); | |
return 0; | |
} | |
printf("S\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment