結果
問題 | No.763 Noelちゃんと木遊び |
ユーザー | omochanal |
提出日時 | 2018-12-20 17:21:09 |
言語 | C++11 (gcc 11.4.0) |
結果 |
AC
|
実行時間 | 128 ms / 2,000 ms |
コード長 | 3,701 bytes |
コンパイル時間 | 1,453 ms |
コンパイル使用メモリ | 167,856 KB |
実行使用メモリ | 35,496 KB |
最終ジャッジ日時 | 2024-09-25 09:06:08 |
合計ジャッジ時間 | 4,614 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 63 ms
35,496 KB |
testcase_01 | AC | 41 ms
23,496 KB |
testcase_02 | AC | 94 ms
27,908 KB |
testcase_03 | AC | 62 ms
25,292 KB |
testcase_04 | AC | 44 ms
24,004 KB |
testcase_05 | AC | 62 ms
25,332 KB |
testcase_06 | AC | 121 ms
29,548 KB |
testcase_07 | AC | 126 ms
29,208 KB |
testcase_08 | AC | 66 ms
25,704 KB |
testcase_09 | AC | 45 ms
23,744 KB |
testcase_10 | AC | 22 ms
21,756 KB |
testcase_11 | AC | 128 ms
29,728 KB |
testcase_12 | AC | 110 ms
28,672 KB |
testcase_13 | AC | 99 ms
28,524 KB |
testcase_14 | AC | 96 ms
27,624 KB |
testcase_15 | AC | 60 ms
25,216 KB |
testcase_16 | AC | 16 ms
20,996 KB |
testcase_17 | AC | 60 ms
25,348 KB |
testcase_18 | AC | 118 ms
29,684 KB |
testcase_19 | AC | 99 ms
28,828 KB |
testcase_20 | AC | 96 ms
28,712 KB |
ソースコード
#include <bits/stdc++.h> #define ADD(a, b) a = (a + ll(b)) % mod #define MUL(a, b) a = (a * ll(b)) % mod #define MAX(a, b) a = max(a, b) #define MIN(a, b) a = min(a, b) #define rep(i, a, b) for(int i = int(a); i < int(b); i++) #define rer(i, a, b) for(int i = int(a) - 1; i >= int(b); i--) #define all(a) (a).begin(), (a).end() #define sz(v) (int)(v).size() #define pb push_back #define sec second #define fst first #define debug(fmt, ...) Debug(__LINE__, ":", fmt, ##__VA_ARGS__) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pi; typedef pair<ll, ll> pl; typedef pair<int, pi> ppi; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<vl> mat; typedef complex<double> comp; void Debug() {cout << '\n'; } template<class FIRST, class... REST>void Debug(FIRST arg, REST... rest){ cout<<arg<<" ";Debug(rest...);} template<class T>ostream& operator<<(ostream& out,const vector<T>& v) { out<<"[";if(!v.empty()){rep(i,0,sz(v)-1)out<<v[i]<<", ";out<<v.back();}out<<"]";return out;} template<class S, class T>ostream& operator<<(ostream& out,const pair<S, T>& v){ out<<"("<<v.first<<", "<<v.second<<")";return out;} const int MAX_N = 300010; const int MAX_V = 100010; const double eps = 1e-6; const ll mod = 1000000007; const int inf = 1 << 30; const ll linf = 1LL << 60; const double PI = 3.14159265358979323846; /////////////////////////////////////////////////////////////////////////////////////////////////// namespace MF { //init before you use it. when you use double, be careful. struct edge { int to, cap, rev; }; vector<edge> G[MAX_N]; int level[MAX_N]; int iter[MAX_N]; void init(int n) { rep(i, 0, n) G[i].clear(); } void add_edge(int from, int to, int cap) { G[from].push_back((edge){to, cap, (int)G[to].size()}); G[to].push_back((edge){from, 0, (int)G[from].size() - 1}); } void bfs(int s) { memset(level, -1, sizeof(level)); queue<int> que; level[s] = 0; que.push(s); while(!que.empty()) { int v = que.front(); que.pop(); for(int i = 0; i < (int)G[v].size(); i++) { edge &e = G[v][i]; if(e.cap > 0 && level[e.to] == -1) { level[e.to] = level[v] + 1; que.push(e.to); } } } } int dfs(int v, int t, int f) { if(v == t) return f; for(int &i = iter[v]; i < (int)G[v].size(); i++) { edge &e = G[v][i]; if(e.cap > 0 && level[v] < level[e.to]) { int d = dfs(e.to, t, min(f, e.cap)); if(d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } ll get(int s, int t) { ll flow = 0; while(true) { bfs(s); if(level[t] == -1) return flow; memset(iter, 0, sizeof(iter)); int f; while((f = dfs(s, t, inf)) > 0) { flow += f; } } } } int N; bool color[MAX_N]; vi G[MAX_N]; void loop(int v, int p, int k) { color[v] = k; rep(i, 0, sz(G[v])) { int n = G[v][i]; if(n == p) continue; loop(n, v, k ^ 1); } } void solve() { cin >> N; int S = N, T = N + 1; MF::init(N + 2); rep(i, 0, N - 1) { int a, b; cin >> a >> b; a--; b--; G[a].pb(b); G[b].pb(a); } loop(0, -1, 0); rep(i, 0, N) { if(!color[i]) { rep(j, 0, sz(G[i])) { int n = G[i][j]; MF::add_edge(i, n, 1); } MF::add_edge(S, i, 1); } else { MF::add_edge(i, T, 1); } } cout << N - MF::get(S, T) << "\n"; } int main() { #ifndef LOCAL ios::sync_with_stdio(false); cin.tie(0); #endif cout << fixed; cout.precision(20); srand((unsigned int)time(NULL)); #ifdef LOCAL //freopen("in.txt", "wt", stdout); //for tester freopen("in.txt", "rt", stdin); #endif solve(); #ifdef LOCAL cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"; #endif return 0; }