1 條題解

  • 0
    @ 2026-2-7 16:13:29
    #include<bits/stdc++.h>
    using namespace std;
    const int N=100100;
    struct cow{
    	int f,s;
    }c[N];
    int h[N], res[N],n,m,cur;
    void solve(int x,int y){
    	if(h[y] == 0) {
    		h[y]=x;
    		cur++;
    	}
    	else if(h[y]>x){
    		int z=h[y];
    		h[y]=x;
    		if(y == c[z].f) solve(z,c[z].s);
    	}
    }
    int main()
    {
    	
    	cin>>n>>m;
    	for(int i=1;i <= n;i++) cin>>c[i].f>>c[i].s;
    	for(int i=n-1;i>=0;i -- ) solve(i+1,c[i+1].f),res[i+1]=cur;
    	for(int i=1;i <= n;i++) cout << res[i] << endl;
    	return 0;
    }
    

    資訊

    ID
    111
    時間
    1000ms
    記憶體
    125MiB
    難度
    4
    標籤
    遞交數
    5
    已通過
    3
    上傳者