結果

問題 No.763 Noelちゃんと木遊び
ユーザー omochanalomochanal
提出日時 2018-12-20 17:21:09
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 116 ms / 2,000 ms
コード長 3,701 bytes
コンパイル時間 2,055 ms
コンパイル使用メモリ 168,880 KB
実行使用メモリ 35,332 KB
最終ジャッジ日時 2023-10-26 01:06:05
合計ジャッジ時間 4,512 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 57 ms
35,332 KB
testcase_01 AC 37 ms
23,352 KB
testcase_02 AC 86 ms
27,832 KB
testcase_03 AC 55 ms
25,208 KB
testcase_04 AC 39 ms
23,788 KB
testcase_05 AC 56 ms
25,148 KB
testcase_06 AC 107 ms
29,504 KB
testcase_07 AC 109 ms
29,160 KB
testcase_08 AC 58 ms
25,540 KB
testcase_09 AC 38 ms
23,768 KB
testcase_10 AC 19 ms
21,860 KB
testcase_11 AC 116 ms
29,556 KB
testcase_12 AC 93 ms
28,480 KB
testcase_13 AC 87 ms
28,456 KB
testcase_14 AC 79 ms
27,536 KB
testcase_15 AC 56 ms
25,180 KB
testcase_16 AC 15 ms
21,296 KB
testcase_17 AC 55 ms
25,188 KB
testcase_18 AC 103 ms
29,516 KB
testcase_19 AC 92 ms
28,776 KB
testcase_20 AC 87 ms
28,784 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}

0