A - Black and White Tree
There is a tree withNNvertices numbered11throughNN. Theii-th of theN−1N−1edges connects verticesaiaiandbibi.
Initially, each vertex is uncolored.
Takahashi and Aoki is playing a game by painting the vertices. In this game, they alternately perform the following operation, starting from Takahashi:
- Select a vertex that is not painted yet.
- If it is Takahashi who is performing this operation, paint the vertex white; paint it black if it is Aoki.
Then, after all the vertices are colored, the following procedure takes place:
- Repaint every white vertex that is adjacent to a black vertex, in black.
Note that all such white vertices are repainted simultaneously, not one at a time.
If there are still one or more white vertices remaining, Takahashi wins; if all the vertices are now black, Aoki wins. Determine the winner of the game, assuming that both persons play optimally.
Constraints
- 2≤N≤1052≤N≤105
- 1≤ai,bi≤N1≤ai,bi≤N
- ai≠biai≠bi
- The input graph is a tree.
Input
Input is given from Standard Input in the following format:
NN a1a1 b1b1 : aN−1aN−1 bN−1bN−1
Output
PrintFirst
if Takahashi wins; printSecond
if Aoki wins.
Sample Input 1
3 1 2 2 3
Sample Output 1
First
Below is a possible progress of the game:
- First, Takahashi paint vertex22white.
- Then, Aoki paint vertex11black.
- Lastly, Takahashi paint vertex33white.
In this case, the colors of vertices11,22and33after the final procedure are black, black and white, resulting in Takahashi's victory.
Sample Input 2
4 1 2 2 3 2 4
Sample Output 2
First
Sample Input 3
6 1 2 2 3 3 4 2 5 5 6
Sample Output 3
Second
#include <iostream> #include <vector> #include <algorithm> #include <string> #include <set> #include <queue> #include <map> #include <sstream> #include <cstdio> #include <cstring> #include <numeric> #include <cmath> #include <iomanip> #include <deque> #include <bitset> #include <cassert> //#include <unordered_set> //#include <unordered_map> #define ll long long #define pii pair<int, int> #define rep(i,a,b) for(int i=a;i<=b;i++) #define dec(i,a,b) for(int i=a;i>=b;i--) #define forn(i, n) for(int i = 0; i < int(n); i++) using namespace std; int dir[4][2] = { { 1,0 },{ 0,1 } ,{ 0,-1 },{ -1,0 } }; const long long INF = 0x7f7f7f7f7f7f7f7f; const int inf = 0x3f3f3f3f; const double pi = acos(-1.0); const double eps = 1e-6; const ll mod = 998244353; inline ll read() { ll x = 0; bool f = true; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); } while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); return f ? x : -x; } ll gcd(ll m, ll n) { return n == 0 ? m : gcd(n, m % n); } /**********************************************************/ const int N = 1e5 + 5; vector<int> g[N]; bool leaf[N]; bool dfs(int x, int fa) { if (g[x].size() == 1 && g[x][0] == fa) { leaf[x] = 1; return 0; } int cnt0 = 0; for (auto to : g[x]) { if (to != fa) { if (dfs(to, x)) return 1; if(leaf[to]) cnt0++; } } if (cnt0 > 1) return 1; if (fa == -1 && cnt0 == 0) return 1; if (cnt0) leaf[x] = 0; else leaf[x] = 1; return 0; } int main() { #ifdef _DEBUG freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int n; cin >> n; rep(i, 1, n-1) { int u, v; scanf("%d%d",&u,&v); g[u].push_back(v); g[v].push_back(u); } int fg = 0; rep(i, 1, n) { if (g[i].size() > 1) { fg = dfs(i, -1); break; } } if (fg) puts("First"); else puts("Second"); return 0; }