Problem:
Problem J: Cluster Network
Problem
マイヅ大学にある大規模クラスタ
†
型スーパーコンピュータ（通称「SAKURA」）のクラスタは、$N$台のコンピュータ（ノード）と$M$本の相互に通信が可能な物理的通信路で構成される。また、各ノードにはそれぞれ1から$N$までの識別番号が割り当てられている。
この度SAKURAの利用を大学関係者だけでなく、県内の企業にも認めることとなった。それに伴い、SAKURAを構成する各ノードを点検する必要がある。しかし、SAKURAの利用スケジュールは数ヶ月先まで埋まっているので、点検中もシステム全体を停止させることはできない。そこで、ノードを1日に1台ずつ順番に点検する。
点検は$N$日にわたって行われ、$i$日目にノード$i$を点検する。このとき、点検するノードとそのノードに直接接続されている通信路をSAKURAのクラスタから切り離す。それによって、SAKURAのクラスタ全体は1つ以上のクラスタに分断されるので、そのうちのいずれかをSAKURAの代わりに運用する。当然、点検中のノードは運用することができない。
複数のクラスタに分断された場合、それらの中で最も「総合性能値」の高いクラスタのみを運用する。各クラスタの「総合性能値」はそれを構成する各ノードに設定された「単体性能値」の和である。
SAKURAを構成するノードの数、通信路の本数と各ノードに設定された「単体性能値」が与えられるので、$i$日目の点検中に運用されるクラスタの「総合性能値」を出力せよ。
†：クラスタとは、1つ以上のコンピュータを結合してひとまとまりにしたシステムのこと。ここでは、物理的通信路で連結なノードの集合とする。
Input
入力は以下の形式で与えられる。
$N$ $M$
$w_1$ $w_2$ ... $w_N$
$u_1$ $v_1$
$u_2$ $v_2$
...
$u_M$ $v_M$
1行目にクラスタを構成するノードの数$N$と通信路の本数$M$が空白区切りで与えられる。
2行目には各ノードの「単体性能値」を表す$N$個の整数が空白区切りで与えられる。$i$番目の整数$w_i$はノード$i$の「単体性能値」を表す。
3行目からの$M$行には各通信路がつなぐ2つのノードの識別番号が空白区切りで与えられる。2+$i$行目の入力では、通信路がノード$u_i$とノード$v_i$を相互につなぐことを表す。
Constraints
入力は以下の条件を満たす。
$2 \leq N \leq 10^5$
$N-1 \leq M \leq min(\frac{N\times(N-1)}{2},10^5)$
$1 \leq w_i \leq 10^6$
$1 \leq u_i, v_i \leq N, u_i \ne v_i$
$(u_i,v_i) \ne (u_j,v_j), (u_i,v_i) \ne (v_j,u_j), i \ne j$
入力は全て整数
Output
出力は$N$行からなる。
$i$行目には、$i$日目に運用されるクラスタの「総合性能値」を出力する。
Sample Input 1
9 10
1 2 3 4 5 6 7 8 9
1 2
2 3
2 4
3 4
4 5
4 6
2 7
7 8
7 9
9 1
Sample Output 1
44
25
42
30
40
39
30
37
36
Sample Input 2
16 19
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 2
2 3
3 4
3 5
1 6
6 7
6 8
7 8
8 9
8 10
10 11
11 12
12 10
1 13
13 14
14 15
15 13
14 16
15 16
Sample Output 2
63
122
124
132
131
73
129
86
127
103
125
124
78
122
121
120
Sample Input 3
2 1
1 2
1 2
Sample Output 3
2
1

Submitted solutions:

code_snippet: // AOJ #3021 Making Pairs
// 2025.3.25

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define gc() getchar_unlocked()
#define pc(c) putchar_unlocked(c)

int Cin() {
	int n = 0; int c = gc();
	do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

void Cout(int n) {
	char b[30];
	if (!n) pc('0');
	else {
		if (n < 0) pc('-'), n = -n;
		int i = 0; while (n) b[i++] = n % 10 + '0', n /= 10;
		while (i--) pc(b[i]);
	}
	pc('\n');
}


struct E { int to, cap, rev; };

int S, T;
vector<vector<E>> g;
vector<int> lev, it;

void add_edge(int s, int t, int cap) {
    g[s].push_back({t, cap, (int)g[t].size()});
    g[t].push_back({s, 0, (int)g[s].size() - 1});
}

bool bfs(int s, int t) {
    fill(lev.begin(), lev.end(), -1);
    queue<int> qu;
    lev[s] = 0;
    qu.push(s);
    while(!qu.empty()){
        int v = qu.front();
        qu.pop();
        for(auto &e : g[v]){
            if(e.cap && lev[e.to] < 0){
                lev[e.to] = lev[v] + 1;
                qu.push(e.to);
            }
        }
    }
    return lev[t] >= 0;
}

int dfs(int v, int t, int f) {
    if(v == t) return f;
    for(int &i = it[v]; i < g[v].size(); i++){
        E &e = g[v][i];
        if(e.cap && lev[v] + 1 == lev[e.to]){
            int ret = dfs(e.to, t, min(f, e.cap));
            if(ret){
                e.cap -= ret;
                g[e.to][e.rev].cap += ret;
                return ret;
            }
        }
    }
    return 0;
}

int main(){
    int n = Cin();
    vector<int> p(n+1);
    p[0] = -1;
    for (int i = 1; i <= n; i++) p[i] = Cin();

    vector<int> dep(n+1, 0);
    dep[0] = 0;
    for (int i = 1; i <= n; i++) dep[i] = dep[p[i]] + 1;

    int tot = n + 3;
    S = n + 1; T = n + 2;
    g.assign(tot, vector<E>());
    lev.assign(tot, -1);
    it.assign(tot, 0);

    int match = 0;
    if(dep[0] % 2 == 0) add_edge(S, 0, 1);
    else add_edge(0, T, 1);

    for (int i = 1; i <= n; i++){
        if(dep[i] % 2 == 0) add_edge(S, i, 1);
        else add_edge(i, T, 1);

        int u = p[i];
        if(dep[u] % 2 == 0) add_edge(u, i, 1);
        else                add_edge(i, u, 1);

        while(bfs(S, T)){
            fill(it.begin(), it.end(), 0);
            while (int f = dfs(S, T, INT_MAX)) match += f;
        }
        Cout(match);
    }
    return 0;
}
accuracy: 1
time_ms: 180
memory_kb: 3924
final_rank: 14

code_snippet: #include <bits/stdc++.h>
using namespace std;

template <class T>
struct Dinic {
  struct edge {
    int to;
    T cap;
    int rev;
    edge(int t = 0, T c = 0, int r = -1) : to(t), cap(c), rev(r) {}
  };
  int n;
  T finf;
  vector<vector<edge>> g;
  vector<int> id;
  vector<int> d;
  Dinic(int _n = 1, T _finf = (int)(2e9))
      : n(_n), finf(_finf), g(n, vector<edge>()), id(n), d(n) {}
  int add(int st, int go, T cap) {
    g[st].emplace_back(go, cap, (int)g[go].size());
    g[go].emplace_back(st, 0, (int)g[st].size() - 1);
    return (int)g[st].size() - 1;
  }
  void bfs(int st) {
    d.assign(n, -1);
    queue<int> qu;
    d[st] = 0;
    qu.push(st);
    while (qu.size()) {
      int now = qu.front();
      qu.pop();
      for (auto e : g[now])
        if (e.cap > 0 && d[e.to] < 0) {
          d[e.to] = d[now] + 1;
          qu.push(e.to);
        }
    }
  }
  T pathdfs(int now, int goal, T nf) {
    if (now == goal) return nf;
    int len = g[now].size();
    for (int& i = id[now]; i < len; ++i) {
      edge* e = &g[now][i];
      if (e->cap > 0 && d[now] < d[e->to]) {
        T res = pathdfs(e->to, goal, min(nf, e->cap));
        if (res > 0) {
          e->cap -= res;
          g[e->to][e->rev].cap += res;
          return res;
        }
      }
    }
    return 0;
  }
  T sole(int start, int goal, T flimit = 0) {
    if (!flimit) flimit = finf;
    T res = 0, nf = 0;
    while (flimit - res) {
      bfs(start);
      if (d[goal] < 0) return res;
      id.assign(n, 0);
      while ((nf = pathdfs(start, goal, flimit - res)) > 0) res += nf;
    }
    return res;
  }
};

int n;
vector<int> p, dist;
Dinic<int> din;

int main() {
  cin >> n;
  p.resize(n);
  dist.assign(n + 1, 0);
  din = Dinic<int>(n + 3);
  din.add(n + 1, 0, 1);
  for (int i = 0; i < n; ++i) {
    cin >> p[i];
    dist[i + 1] = dist[p[i]] ^ 1;
    if (dist[i + 1])
      din.add(i + 1, n + 2, 1);
    else
      din.add(n + 1, i + 1, 1);
  }
  int now = 0;
  for (int i = 0; i < n; ++i) {
    if (dist[i + 1])
      din.add(p[i], i + 1, 1);
    else
      din.add(i + 1, p[i], 1);
    now += din.sole(n + 1, n + 2);
    cout << now << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 280
memory_kb: 4000
final_rank: 15

code_snippet: #include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
using namespace std;

typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
const ll mod = 1000000007;
typedef double ld;
typedef complex<ld> Point;
const ll INF = mod * mod;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
typedef vector<int> vec;
typedef vector<string> svec;

vector<int> G[5001];
bool used[5001]; int ans;
void dfs(int id, int fr) {
	bool f = false;
	rep(j, G[id].size()) {
		int to = G[id][j];
		dfs(to, id);
		if (!used[to]) {
			f = true;
		}
	}
	if (f) {
		used[id] = true; ans++;
	}

}
void solve() {
	int n;
	cin >> n;
	rep1(i, n) {
		int p; cin >> p;
		G[p].push_back(i);
		fill(used, used + n + 1, false);
		ans = 0;
		dfs(0, -1);
		//cout << "ans is ";
		cout << ans << endl;
	}
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(5);
	//while (cin >> n>>h>>m>>s,n)solve();
	solve();
	//stop
	return 0;
}
accuracy: 1
time_ms: 110
memory_kb: 3412
final_rank: 4

code_snippet: #include <bits/stdc++.h>

#define _overload(_1,_2,_3,name,...) name
#define _rep(i,n) _range(i,0,n)
#define _range(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)

#define _rrep(i,n) _rrange(i,n,0)
#define _rrange(i,a,b) for(int i=int(a)-1;i>=int(b);--i)
#define rrep(...) _overload(__VA_ARGS__,_rrange,_rrep,)(__VA_ARGS__)

#define _all(arg) begin(arg),end(arg)
#define uniq(arg) sort(_all(arg)),(arg).erase(unique(_all(arg)),end(arg))
#define getidx(ary,key) lower_bound(_all(ary),key)-begin(ary)
#define clr(a,b) memset((a),(b),sizeof(a))
#define bit(n) (1LL<<(n))
#define popcount(n) (__builtin_popcountll(n))

using namespace std;

template<class T>bool chmax(T &a, const T &b) { return (a < b) ? (a = b, 1) : 0;}
template<class T>bool chmin(T &a, const T &b) { return (b < a) ? (a = b, 1) : 0;}

using ll = long long;
using R = long double;
const R EPS = 1e-9L; // [-1000,1000]->EPS=1e-8 [-10000,10000]->EPS=1e-7
inline int sgn(const R& r) {return (r > EPS) - (r < -EPS);}
inline R sq(R x) {return sqrt(max(x, 0.0L));}

const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

// Problem Specific Parameter:

const int limit = 5010;
vector<int> graph[limit];

bool used[limit];
int match[limit];

bool dfs(int v) {
	used[v] = true;
	for (auto &u : graph[v]) {
		const int w = match[u];
		if (w == -1 or (used[w] == false and dfs(w))) {
			match[v] = u;
			match[u] = v;
			return true;
		}
	}
	return false;
}

int main(void) {
	int n;
	cin >> n;

	fill_n(match, n + 1, -1);
	int ans = 0;

	rep(v, 1, n + 1) {
		int u;
		cin >> u;
		graph[u].push_back(v);
		graph[v].push_back(u);
		fill_n(used, n + 1, false);
		if (dfs(v)) ans++;
		cout << ans << endl;
	}

	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3468
final_rank: 2

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

enum Type{
	Call,
	Called_or_Lonely,
};

int N,memo[5001][2];

vector<int> V[5001];

int recursive(int node_id,Type type){

	if(memo[node_id][type] != -1)return memo[node_id][type];

	int ret = 0,tmp,sum;

	switch(type){
	case Call:
		sum = 0;
		for(int i = 0; i < V[node_id].size(); i++){
			sum += max(recursive(V[node_id][i],Call),recursive(V[node_id][i],Called_or_Lonely));
		}
		for(int i = 0; i < V[node_id].size(); i++){
			ret = max(ret,sum-max(memo[V[node_id][i]][Call],memo[V[node_id][i]][Called_or_Lonely])+recursive(V[node_id][i],Called_or_Lonely)+1);
		}
		break;
	case Called_or_Lonely:
		for(int i = 0; i < V[node_id].size(); i++){
			ret += max(recursive(V[node_id][i],Call),recursive(V[node_id][i],Called_or_Lonely));
		}
		break;
	}

	return memo[node_id][type] = ret;
}

int func(){

	return max(recursive(0,Call),recursive(0,Called_or_Lonely));
}


int main(void){

	scanf("%d",&N);
	int tmp;

    for(int day = 1;day <= N; day++){

        scanf("%d",&tmp);
        V[tmp].push_back(day);
        for(int i = 0; i <= day; i++){
        	for(int k = 0; k < 3; k++)memo[i][k] = -1;
        }

        printf("%d\n",func());
    }
    return 0;
}
accuracy: 1
time_ms: 360
memory_kb: 3488
final_rank: 17

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

enum Type{
	Call,
	Called,
	Lonely,
};

int N,memo[5001][3];

vector<int> V[5001];

int recursive(int node_id,Type type){

	if(memo[node_id][type] != -1)return memo[node_id][type];

	int ret = 0,tmp,sum;

	switch(type){
	case Call:
		sum = 0;
		for(int i = 0; i < V[node_id].size(); i++){
			sum += max(recursive(V[node_id][i],Call),recursive(V[node_id][i],Lonely));
		}
		for(int i = 0; i < V[node_id].size(); i++){
			ret = max(ret,sum-max(memo[V[node_id][i]][Call],memo[V[node_id][i]][Lonely])+recursive(V[node_id][i],Called)+1);
		}
		break;
	case Called:
		for(int i = 0; i < V[node_id].size(); i++){
			ret += max(recursive(V[node_id][i],Call),recursive(V[node_id][i],Lonely));
		}
		break;
	case Lonely:
		for(int i = 0; i < V[node_id].size(); i++){
			ret += max(recursive(V[node_id][i],Call),recursive(V[node_id][i],Lonely));
		}
		break;
	}

	return memo[node_id][type] = ret;
}

int func(){

	return max(recursive(0,Call),recursive(0,Lonely));
}


int main(void){

	scanf("%d",&N);
	int tmp;

    for(int day = 1;day <= N; day++){

        scanf("%d",&tmp);
        V[tmp].push_back(day);
        for(int i = 0; i <= day; i++){
        	for(int k = 0; k < 3; k++)memo[i][k] = -1;
        }

        printf("%d\n",func());
    }
    return 0;
}
accuracy: 1
time_ms: 480
memory_kb: 3512
final_rank: 18

code_snippet: #include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

int n;
int dp[5001][2];
vector<int> G[5001];

void dfs(int v){
	int sum=0;
	for(int i=0;i<G[v].size();i++){
		dfs(G[v][i]);
		sum+=max(dp[G[v][i]][0],0);
	}
	dp[v][1]=sum;
	int tmp=0;
	for(int i=0;i<G[v].size();i++){
		tmp=max(dp[G[v][i]][1]+1-dp[G[v][i]][0],tmp);
	}
	dp[v][0]=sum+tmp;
}

int main(void){
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		int p;
		scanf("%d",&p);
		G[p].push_back(i+1);
		memset(dp,-1,sizeof(dp));
		dfs(0);
		printf("%d\n",max(dp[0][0],dp[0][1]));
	}
	return 0;
}
accuracy: 1
time_ms: 160
memory_kb: 3480
final_rank: 13

code_snippet: #include<bits/stdc++.h>
using namespace std;
int N;
vector<int> G[5001];
int A[5001],B[5001];

void dfs(int pos){
  A[pos]=B[pos]=0;
  int maxm=-1e9, sum=0;
  for(int i=0;i<(int)G[pos].size();i++){
    int to=G[pos][i];
    dfs(to);
    B[pos]+=A[to];
    sum+=A[to];
    maxm=max(maxm,-A[to]+B[to]);
  }
  A[pos]=max(sum,sum+maxm+1);
}

int solve(){
  dfs(0);
  return A[0];
}

int main(){
  cin>>N;
  for(int i=1;i<=N;i++){
    int id;
    cin>>id;
    G[id].push_back(i);
    printf("%d\n",solve());
  }
  return 0;
}
accuracy: 1
time_ms: 150
memory_kb: 3492
final_rank: 12

code_snippet: #include<bits/stdc++.h>
using namespace std;
signed main(){
  int n;
  cin>>n;
  vector<int> G[5555];
  int dp[2][5555];
  function<int(int,int)> dfs=[&](int x,int v){
    if(G[v].empty()) return 0;
    if(~dp[x][v]) return dp[x][v];
    int res=0;
    for(int u:G[v]) res+=dfs(0,u);
    if(x) return dp[x][v]=res;
    int tmp=res;
    for(int u:G[v])
      res=max(res,tmp-dfs(0,u)+dfs(1,u)+1);
    return dp[x][v]=res;
  };
  for(int i=1;i<=n;i++){
    int p;
    cin>>p;
    G[p].push_back(i);
    memset(dp,-1,sizeof(dp));
    cout<<dfs(0,0)<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 360
memory_kb: 3360
final_rank: 16

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define int long long

struct BipartiteMatching{
  vector<vector<int> > G;
  int V;
  vector<int> match,used;
  
  BipartiteMatching(){}
  BipartiteMatching(int V):V(V){init();}
  
  void init(){
    for(int i=0;i<(int)G.size();i++) G[i].clear();
    G.clear();
    match.clear();
    used.clear();
    G.resize(V);
    match.resize(V,-1);
    used.resize(V,0);
  }
  
  void add_edge(int u,int v){
    G[u].push_back(v);
    G[v].push_back(u);
  }

  bool dfs(int v){
    used[v]=true;
    for(int i=0;i<(int)G[v].size();i++){
      int u=G[v][i],w=match[u];
      if(w<0||(!used[w]&&dfs(w))){
	match[v]=u;
	match[u]=v;
	return true;
      }
    }
    return false;
  }

  int calc(int v){
    int res=0;
    //fill(match.begin(),match.end(),-1);
    fill(used.begin(),used.end(),0);
    if(dfs(v)){
      res++;
    }
    return res;
  }
};


signed main(){
  int n;
  cin>>n;
  BipartiteMatching bm(n+1);
  int ans=0;
  for(int i=1;i<=n;i++){
    int p;
    cin>>p;
    bm.add_edge(p,i);
    ans+=bm.calc(i);
    cout<<ans<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3256
final_rank: 1

code_snippet: #include <bits/stdc++.h>
#define REP(i, a, n) for(ll i = ((ll) a); i < ((ll) n); i++)
#define INF (1LL << 30)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;

ll N, P[5001];
vector<ll> E[5001];

pll dfs(ll v, ll p) {
  ll cnt = 0, t = 1;
  for(ll u : E[v]) {
    if(u == p) continue;
    pll d = dfs(u, v);
    cnt += d.first;
    if(d.second == 1) t = 0;
  }
  if(t == 0) cnt++;

  // cout << v << ": " << cnt << " " << t << endl;

  return pll(cnt, t);
}

int main(void) {
  cin >> N;
  REP(i, 1, N + 1) cin >> P[i];

  REP(i, 1, N + 1) {
    E[P[i]].push_back(i);
    E[i].push_back(P[i]);

    cout << dfs(i, -1).first << endl;
  }
}
accuracy: 1
time_ms: 120
memory_kb: 3484
final_rank: 6

code_snippet: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define INF 1<<30
#define LINF 1LL<<60

bool match[5050][5050];
int ans = 0;
bool dfs(int u, int k, vector<int>rev, vector<vector<int>>& G) {
	rev.push_back(u);
	bool f = false;
	for (int i = 0; i < G[u].size();i++) {
		int v = G[u][i]; 
		if (rev.size() > 1) {
			if (v == rev[rev.size() - 2])continue;
		}
		if (k == 0 && !match[u][v]) {
			f = true;
			if (dfs(v, 1, rev, G))return true;
		}
		if (k == 1 && match[u][v]) {
			f = true;
			if (dfs(v, 0, rev, G))return true;
		}
	}
	if (!f && k == 1) {
		int cnt = 0;
		for (int i = rev.size() - 1; i > 0;i--) {
			if (cnt % 2 == 0) {
				if (!match[rev[i]][rev[i - 1]]) { ans++;}
				match[rev[i]][rev[i - 1]] = match[rev[i - 1]][rev[i]] = true;
			}
			else {
				if (match[rev[i]][rev[i - 1]]) { ans--; }
				match[rev[i]][rev[i - 1]] = match[rev[i - 1]][rev[i]] = false;
			}
			cnt++;
		}
		return true;
	}
	return false;
}

int main() {
	cin.tie(0); ios::sync_with_stdio(false);
	int N; cin >> N;
	vector<vector<int>> G(N+1);
	for (int v = 0; v < N;v++) {
		int p; cin >> p;
		G[p].push_back(v + 1);
		G[v + 1].push_back(p);
		vector<int> rev;
		dfs(v + 1, 0, rev, G);
		cout << ans << endl;
	}

	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 20588
final_rank: 19

code_snippet: #include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

//#define int long long
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9) + 7;
template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; }

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int N; cin >> N;
	vector<vector<int>> g(N + 1);
	vector<int> p(N); rep(i, 0, N) {
		cin >> p[i];
		g[p[i]].emplace_back(i + 1);

		function<int(int)> dfs = [&](int x) {
			int ret = 0;
			bool odd = false;
			rep(i, 0, g[x].size()) {
				int r = dfs(g[x][i]);
				if (r % 2 == 1)
					odd = true;
				ret += (r / 2) * 2;
			}
			if (odd)
				ret += 2;
			else
				ret += 1;
			return ret;
		};

		int maxi = dfs(0);
		dump(maxi);
		cout << maxi / 2 << endl;

	}

	return 0;
}
accuracy: 1
time_ms: 130
memory_kb: 3268
final_rank: 7

code_snippet: #include<bits/stdc++.h>
using namespace std;

#define int long long

#define rep(i,n) for(int i=0;i<(n);i++)
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
typedef vector<int>vint;
typedef pair<int,int>pint;
typedef vector<pint>vpint;

template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}

int N;
vint G[5555];

int dp[5555],topused[5555];

void dfs(int v,int p){
    dp[v]=0;
    topused[v]=0;

    for(auto u:G[v]){
        if(u==p)continue;
        dfs(u,v);
        dp[v]+=dp[u];
        topused[v]|=!topused[u];
    }

    if(topused[v])dp[v]++;
}

signed main(){
    cin>>N;

    for(int i=1;i<=N;i++){
        int p;cin>>p;
         G[p].pb(i);
         dfs(0,-1);
         cout<<dp[0]<<endl;
    }
    return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 3432
final_rank: 5

code_snippet: #include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)

using namespace std;
typedef long long int ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<int, int> PI;
const ll mod = 1e9 + 7;

const int N = 5124;
int par[N];
VI edges[N];

PI dfs(int n, int v) {
  int tot = 0;
  bool some = false;
  REP(i, 0, edges[v].size()) {
    int w = edges[v][i];
    PI res = dfs(n, w);
    if (res.second) {
      some = true;
    }
    tot += res.first;
  }
  return PI(tot + (some ? 1 : 0), some ? 0 : 1);
}

int calc(int n) {
  return dfs(n, 0).first;
}


int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  par[0] = -1;
  REP(i, 0, n) {
    int pp;
    cin >> pp;
    par[i + 1] = pp;
    edges[pp].push_back(i + 1);
    cout << calc(i + 2) << "\n";
  }
}
accuracy: 1
time_ms: 90
memory_kb: 3380
final_rank: 3

code_snippet: // clang-format off
#include <bits/stdc++.h>
#define int long long
#define main signed main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define forever for (;;)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
template<typename A> using V = std::vector<A>;
template<typename A> using F = std::function<A>;
template<typename A, typename B> using P = std::pair<A, B>;
using pii = P<int, int>;
using vi = V<int>;
using vd = V<double>;
using vs = V<std::string>;
using vpii = V<pii>;
using vvi = V<vi>;
using vvpii = V<vpii>;
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979;
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
constexpr bool odd(const int n) { return n & 1; }
constexpr bool even(const int n) { return ~n & 1; }
template<typename T> std::istream &operator>>(std::istream &is, std::vector<T> &v) { for (T &x : v) is >> x; return is; }
template<typename A, typename B> std::istream &operator>>(std::istream &is, std::pair<A, B> &p) { is >> p.first; is >> p.second; return is; }
using namespace std;
// clang-format on

static vi child[5010];
int dp[5010][2];
void dfs(int v) {
  dp[v][0] = dp[v][1] = 0;
  int S = 0;
  for (int u : child[v]) {
    dfs(u);
    S += max(dp[u][0], dp[u][1]);
  }
  dp[v][0] = S;
  for (int u : child[v]) {
    int T = S - max(dp[u][0], dp[u][1]) + dp[u][0] + 1;
    cmax(dp[v][1], T);
  }
}

main {
  int n;
  cin >> n;
  rep(i, n) {
    int p;
    cin >> p;
    child[p].push_back(i + 1);
    dfs(0);
    cout << max(dp[0][0], dp[0][1]) << endl;
  }
}
accuracy: 1
time_ms: 150
memory_kb: 3448
final_rank: 11

code_snippet: #include <iostream>
#include <vector>

using namespace std;

int N;
vector<vector<int>> t;
vector<bool> used;
int eval(int root) {
	int ret = 0;
	for (int i = 0; i < t[root].size(); i++) {
		int node = t[root][i];
		if (!used[root] && t[node].size() == 0) {
			used[root] = used[node] = true;
			ret++;
		} else ret += eval(node);
		if (!used[root] && !used[node]) {
			used[root] = used[node] = true;
			ret++;
		}
	}
	return ret;
}
int main(void) {
	cin >> N;
	t = vector<vector<int>>(N+1, vector<int>(0));
	for (int i = 1; i <= N; i++) {
		int pi;
		cin >> pi;
		used = vector<bool>(N+1, false);
		t[pi].push_back(i);
		cout << eval(0) << endl;
	}
}
accuracy: 1
time_ms: 150
memory_kb: 3252
final_rank: 10

code_snippet: // ?????????????????????

#include <bits/stdc++.h>

using namespace std;

struct Bipartite_Matching
{
  vector< vector< int > > graph;
  vector< int > match, alive, used;
  int timestamp;

  Bipartite_Matching(int n)
  {
    timestamp = 0;
    graph.resize(n);
    alive.assign(n, 0);
    used.assign(n, 0);
    match.assign(n, -1);
  }

  void add_edge(int u, int v)
  {
    graph[u].push_back(v);
    graph[v].push_back(u);
  }

  bool dfs(int v)
  {
    used[v] = timestamp;
    for(int i = 0; i < graph[v].size(); i++) {
      int u = graph[v][i], w = match[u];
      if(alive[u] == 0) continue;
      if(w == -1 || (used[w] != timestamp && dfs(w))) {
        match[v] = u;
        match[u] = v;
        return (true);
      }
    }
    return (false);
  }

  int bipartite_matching()
  {
    int ret = 0;
    for(int i = 0; i < graph.size(); i++) {
      if(alive[i] == 0) continue;
      if(match[i] == -1) {
        ++timestamp;
        ret += dfs(i);
      }
    }
    return (ret);
  }

  int add(int k)
  {
    alive[k] = 1;
    ++timestamp;
    return (dfs(k));
  }

};

int main()
{
  int N, A[150000], idx[150000];
  vector< int > g[150000];
  bool dep[150000] = {};

  cin >> N;
  ++N;
  for(int i = 1; i < N; i++) {
    cin >> idx[i];
    g[idx[i]].push_back(i);
    dep[i] = dep[idx[i]] ^ 1;
  }

  Bipartite_Matching flow(N);
  vector< int > left, right;
  for(int i = 0; i < N; i++) {
    if(dep[i]) left.push_back(i);
    else right.push_back(i);
  }

  for(int i = 1; i < N; i++) {
    int oi, beet;
    if(dep[i]) oi = lower_bound(begin(left), end(left), i) - begin(left);
    else oi = (int) (lower_bound(begin(right), end(right), i) - begin(right)) + left.size();

    if(dep[idx[i]]) beet = lower_bound(begin(left), end(left), idx[i]) - begin(left);
    else beet = (int) (lower_bound(begin(right), end(right), idx[i]) - begin(right)) + left.size();

    flow.add_edge(oi, beet);
  }
  flow.alive[(int) (lower_bound(begin(right), end(right), 0) - begin(right)) + left.size()] = true;

  flow.bipartite_matching();
  int ret = 0;
  for(int i = 1; i < N; i++) {
    int oi;
    if(dep[i]) oi = lower_bound(begin(left), end(left), i) - begin(left);
    else oi = (int) (lower_bound(begin(right), end(right), i) - begin(right)) + left.size();
    ret += flow.add(oi);
    cout << ret << endl;
  }


}
accuracy: 1
time_ms: 10
memory_kb: 7044
final_rank: 9

code_snippet: // need
#include <iostream>
#include <algorithm>

// data structure
#include <bitset>
//#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <array>
//#include <tuple>
//#include <unordered_map>
#include <unordered_set>
#include <complex>
//#include <deque>
#include<valarray>

// stream
//#include <istream>
//#include <sstream>
//#include <ostream>
//#include <fstream>

// etc
#include <cmath>
#include <cassert>
#include <functional>
#include <iomanip>
//#include <typeinfo>
#include <chrono>
#include <random>
#include <numeric>

// input
#define INIT std::ios::sync_with_stdio(false);std::cin.tie(0);
#define VAR(type, ...)type __VA_ARGS__;MACRO_VAR_Scan(__VA_ARGS__);
template<typename T> void MACRO_VAR_Scan(T& t) { std::cin >> t; }
template<typename First, typename...Rest>void MACRO_VAR_Scan(First& first, Rest&...rest) { std::cin >> first; MACRO_VAR_Scan(rest...); }
#define VEC_ROW(type, n, ...)std::vector<type> __VA_ARGS__;MACRO_VEC_ROW_Init(n, __VA_ARGS__); for(int i=0; i<n; ++i){MACRO_VEC_ROW_Scan(i, __VA_ARGS__);}
template<typename T> void MACRO_VEC_ROW_Init(int n, T& t) { t.resize(n); }
template<typename First, typename...Rest>void MACRO_VEC_ROW_Init(int n, First& first, Rest&...rest) { first.resize(n); MACRO_VEC_ROW_Init(n, rest...); }
template<typename T> void MACRO_VEC_ROW_Scan(int p, T& t) { std::cin >> t[p]; }
template<typename First, typename...Rest>void MACRO_VEC_ROW_Scan(int p, First& first, Rest&...rest) { std::cin >> first[p]; MACRO_VEC_ROW_Scan(p, rest...); }
#define VEC(type, c, n) std::vector<type> c(n);for(auto& i:c)std::cin>>i;
#define MAT(type, c, m, n) std::vector<std::vector<type>> c(m, std::vector<type>(n));for(auto& r:c)for(auto& i:r)std::cin>>i;

// output
#define OUT(d) std::cout<<d;
#define FOUT(n, d) std::cout<<std::fixed<<std::setprecision(n)<<d;
#define SOUT(n, c, d) std::cout<<std::setw(n)<<std::setfill(c)<<d;
#define SP std::cout<<" ";
#define TAB std::cout<<"\t";
#define BR std::cout<<"\n";
#define SPBR(i, n) std::cout<<(i + 1 == n ? '\n' : ' ');
#define ENDL std::cout<<std::endl;
#define FLUSH std::cout<<std::flush;
#define SHOW(d) {std::cerr << #d << "\t:" << d << "\n";}
#define SHOWVECTOR(v) {std::cerr << #v << "\t:";for(const auto& xxx : v){std::cerr << xxx << " ";}std::cerr << "\n";}
#define SHOWVECTOR2(v) {std::cerr << #v << "\t:\n";for(const auto& xxx : v){for(const auto& yyy : xxx){std::cerr << yyy << " ";}std::cerr << "\n";}}
#define SHOWQUEUE(a) {auto tmp(a);std::cerr << #a << "\t:";while(!tmp.empty()){std::cerr << tmp.front() << " ";tmp.pop();}std::cerr << "\n";}

// utility
#define ALL(a) (a).begin(),(a).end()
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define RFOR(i, a, b) for(int i=(b)-1;i>=(a);--i)
#define REP(i, n) for(int i=0;i<int(n);++i)
#define RREP(i, n) for(int i=int(n)-1;i>=0;--i)
#define FORLL(i, a, b) for(ll i=ll(a);i<ll(b);++i)
#define RFORLL(i, a, b) for(ll i=ll(b)-1;i>=ll(a);--i)
#define REPLL(i, n) for(ll i=0;i<ll(n);++i)
#define RREPLL(i, n) for(ll i=ll(n)-1;i>=0;--i)
#define IN(a, x, b) (a<=x && x<b)
template<typename T> inline T CHMAX(T& a, const T b) { return a = (a < b) ? b : a; }
template<typename T> inline T CHMIN(T& a, const T b) { return a = (a > b) ? b : a; }
#define EXCEPTION(msg) throw std::string("Exception : " msg " [ in ") + __func__ + " : " + std::to_string(__LINE__) + " lines ]"
#define TRY(cond, msg) try {if (cond) EXCEPTION(msg);}catch (std::string s) {std::cerr << s << std::endl;}
void CHECKTIME(std::function<void()> f) { auto start = std::chrono::system_clock::now(); f(); auto end = std::chrono::system_clock::now(); auto res = std::chrono::duration_cast<std::chrono::nanoseconds>((end - start)).count(); std::cerr << "[Time:" << res << "ns  (" << res / (1.0e9) << "s)]\n"; }

// test
template<class T> std::vector<std::vector<T>> VV(int n) {
	return std::vector<std::vector<T>>(n);
}
template<class T> std::vector<std::vector<T>> VV(int n, int m, T init = T()) {
	return std::vector<std::vector<T>>(n, std::vector<T>(m, init));
}
template<typename S, typename T>
std::ostream& operator<<(std::ostream& os, std::pair<S, T> p) {
	os << "(" << p.first << ", " << p.second << ")"; return os;
}

// type/const

#define int ll
using ll = long long;
using ull = unsigned long long;
using PAIR = std::pair<int, int>;
using PAIRLL = std::pair<ll, ll>;
constexpr int INFINT = 1 << 30;                          // 1.07x10^ 9
constexpr int INFINT_LIM = (1LL << 31) - 1;              // 2.15x10^ 9
constexpr ll INFLL = 1LL << 60;                          // 1.15x10^18
constexpr ll INFLL_LIM = (1LL << 62) - 1 + (1LL << 62);  // 9.22x10^18
constexpr double EPS = 1e-7;
constexpr int MOD = 1000000007;
constexpr double PI = 3.141592653589793238462643383279;

signed main() {
	INIT;
	VAR(int, n);
	std::vector<std::vector<int>> g(1);
	REP(i, n) {
		VAR(int, in_p);
		g.emplace_back();
		g[i + 1].emplace_back(in_p);
		g[in_p].emplace_back(i + 1);

		std::function<PAIR(int, int)> dfs = [&](int v, int par) {
			PAIR p(0, 1);
			bool use = false;
			bool chi = true;
			for (auto to : g[v]) {
				if (to == par) continue;
				chi = false;
				PAIR pp = dfs(to, v);
				p.first += pp.first;
				if (pp.second == 1 && p.second == 1) {
					p.second = 0;
					++p.first;
				}
			}
			return p;
		};

		OUT(dfs(0, -1).first)BR;
	}
	return 0;
}
accuracy: 1
time_ms: 130
memory_kb: 3428
final_rank: 8

code_snippet: #include<iostream>
#include<vector>
#include<string>
#include<algorithm>	
#include<map>
#include<set>
#include<utility>
#include<cmath>
#include<cstring>
#include<queue>
#include<stack>
#include<cstdio>
#include<sstream>
#include<iomanip>
#include<assert.h>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
using namespace std;
//kaewasuretyuui
typedef long long ll;
typedef int Def;
typedef pair<Def,Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def,pii> pip;
typedef vector<pip>vip;
//#define mt make_tuple
//typedef tuple<double,int,double> tp;
//typedef vector<tp> vt;
template<typename A,typename B>bool cmin(A &a,const B &b){return a>b?(a=b,true):false;}
template<typename A,typename B>bool cmax(A &a,const B &b){return a<b?(a=b,true):false;}
const double PI=acos(-1);
const double EPS=1e-7;
Def inf=sizeof(Def)==sizeof(long long)?2e18:1e9;
int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};
struct edge{int to,cap,rev;};//ikisaki youryou gyakuhen
class MF{//max flow
	public:
	int n;
	vector<vector<edge> >G;//[MAX];
	vi level,iter;//[MAX];
	MF(int size){
		n=size;
		G=vector<vector<edge> >(n);
	}
	void add_edge(int from, int to, int cap){
		edge q={to,cap,int(G[to].size())};
		G[from].push_back(q);
		q={from,0,int(G[from].size()-1)};
		G[to].push_back(q);
	}
	void bfs(int s){
		level=vi(n,-1);
		queue<int>q;
		level[s]=0;
		q.push(s);
		while(!q.empty()){
			int v=q.front();q.pop();
			for(int i=0;i<G[v].size();i++){
				edge &e=G[v][i];
				if(e.cap>0&&level[e.to]<0){
					level[e.to]=level[v]+1;
					q.push(e.to);
				}
			}
		}
	}
	int dfs(int v,int t, int f) {
		if(v==t)return f;
		for(int &i=iter[v];i<G[v].size();i++){
			edge &e=G[v][i];
			if(level[v]>=level[e.to]||e.cap<=0) continue;
			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;
	}
	int mf(int s,int t) {//from s to t,ford_fulkerson
		int flow=0;
		while(1){
			bfs(s);
			if(level[t]<0)return flow;
			iter=vi(n);
			int f;
			while((f=dfs(s,t,inf))>0)flow+=f;
		}
	}
};
int main(){
	int n;
	cin>>n;
	MF mf(2*n+4);
	int s=2*n+2,t=s+1;
	rep(i,n+1)mf.add_edge(s,i,1);
	rep(i,n+1)mf.add_edge(n+i+1,t,1);
	int sum=0;
	rep(i,n){
		int a;cin>>a;
		mf.add_edge(a,n+i+2,1);
		mf.add_edge(i+1,n+a+1,1);
		sum+=mf.mf(s,t);
		cout<<sum/2<<endl;
	}
}
accuracy: 1
time_ms: 630
memory_kb: 3980
final_rank: 20