結果

問題 No.1582 Vertexes vs Edges
ユーザー 沙耶花沙耶花
提出日時 2021-07-02 21:47:01
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 217 ms / 2,000 ms
コード長 1,924 bytes
コンパイル時間 4,576 ms
コンパイル使用メモリ 272,640 KB
実行使用メモリ 31,288 KB
最終ジャッジ日時 2023-09-11 21:37:51
合計ジャッジ時間 9,021 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 5 ms
4,376 KB
testcase_05 AC 103 ms
28,136 KB
testcase_06 AC 6 ms
4,524 KB
testcase_07 AC 15 ms
6,972 KB
testcase_08 AC 51 ms
15,352 KB
testcase_09 AC 97 ms
26,416 KB
testcase_10 AC 60 ms
17,728 KB
testcase_11 AC 8 ms
4,768 KB
testcase_12 AC 41 ms
13,092 KB
testcase_13 AC 92 ms
19,168 KB
testcase_14 AC 96 ms
19,248 KB
testcase_15 AC 138 ms
25,308 KB
testcase_16 AC 60 ms
14,268 KB
testcase_17 AC 89 ms
18,456 KB
testcase_18 AC 152 ms
29,984 KB
testcase_19 AC 81 ms
19,624 KB
testcase_20 AC 86 ms
17,132 KB
testcase_21 AC 70 ms
16,112 KB
testcase_22 AC 132 ms
27,000 KB
testcase_23 AC 46 ms
11,628 KB
testcase_24 AC 21 ms
7,256 KB
testcase_25 AC 73 ms
15,504 KB
testcase_26 AC 54 ms
12,552 KB
testcase_27 AC 145 ms
24,692 KB
testcase_28 AC 35 ms
9,684 KB
testcase_29 AC 112 ms
21,164 KB
testcase_30 AC 69 ms
14,512 KB
testcase_31 AC 123 ms
21,900 KB
testcase_32 AC 45 ms
11,268 KB
testcase_33 AC 211 ms
31,288 KB
testcase_34 AC 208 ms
31,288 KB
testcase_35 AC 217 ms
31,280 KB
testcase_36 AC 1 ms
4,376 KB
testcase_37 AC 1 ms
4,380 KB
testcase_38 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace atcoder;
using mint = modint1000000007;
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf 1000000001

struct lca{
	
	vector<int> depth;
	vector<vector<int>> parents;
	
	int max_j=18;
	
	lca(int n,vector<vector<int>> &E){
		depth.assign(E.size(),-1);
		parents.assign(E.size(),vector<int>(max_j,-1));
		
		rep(i,100){
			if((1<<i)>E.size()){
				max_j = i;
				break;
			}
		}
		
		stack<int> s;
		s.push(n);
		depth[n] = 0;
		while(s.size()!=0){
			int k = s.top();
			s.pop();
			for(int i=0;i<E[k].size();i++){
				int x = E[k][i];
				if(depth[x]!=-1)continue;
				s.push(x);
				depth[x] = depth[k]+1;
				for(int j=0;j<max_j;j++){
					if(j==0){
						parents[x][j] = k;
					}
					else{
						parents[x][j] = parents[parents[x][j-1]][j-1];
					}
					if(parents[x][j] == -1)break;
				}
			}
		}
	}
	
	
	int kth_parent(int u,int k){
		for(int i=0;i<max_j;i++){
			if(k==0)break;
			if(u==-1)break;
			if(k%2){
				u = parents[u][i];
			}
			k/=2;
		}
		return u;
	}
	
	int query(int u,int v){
		if(depth[u]<depth[v])swap(u,v);
		u = kth_parent(u,depth[u]-depth[v]);
		
		if(u==v){
			return u;
		}
		
		for(int i=max_j-1;i>=0;i--){
			if(parents[u][i]!=parents[v][i]){
				u = parents[u][i];
				v = parents[v][i];
			}
		}
		
		return parents[u][0];
		
	}
	
	int get_distance(int u,int v){
		return depth[u]+depth[v]-2*depth[query(u,v)];
	}
	
	
};

int main(){
	
	int N;
	cin>>N;
	
	vector<vector<int>> E(N);
	rep(i,N-1){
		int u,v;
		cin>>u>>v;
		
		u--;v--;
		E[u].push_back(v);
		E[v].push_back(u);
	}
	
	lca L(0,E);
	
	mf_graph<int> G(N+2);
	int S = N,T = N+1;
	
	rep(i,N){
		if(L.get_distance(0,i)%2==0){
			rep(j,E[i].size()){
				int u = i,v = E[i][j];
				G.add_edge(u,v,1);
			}
			G.add_edge(S,i,1);
		}
		else{
			G.add_edge(i,T,1);
		}
	}
	
	cout<<G.flow(S,T)<<endl;
	
		
	
    return 0;
}
0