QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#415843#8716. 树KIRITO1211#WA 1ms3764kbC++232.7kb2024-05-21 11:12:512024-05-21 11:12:51

Judging History

你现在查看的是最新测评结果

  • [2024-05-21 11:12:51]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3764kb
  • [2024-05-21 11:12:51]
  • 提交

answer


// LUOGU_RID: 154919549
//#pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>
#define int long long
using ll = long long;
const int M = 5e3 + 3;
//#define int long long
//int a[M];
//long long s[M],f1[M][M * 2],g[M][M * 2];
using namespace std;
const int mod = 1e9 + 7;
//int a[200050];
void solve() {
    int n,m,q;std::cin>>n>>m>>q;
	std::vector<std::vector<int> > e(n + 2), f(n + 2,std::vector<int>(102));
	std::vector<int> dep(2 + n);    
    for(int i = 1;i < n;i++){
        int u,v;std::cin>>u>>v;
        e[u].push_back(v);
        e[v].push_back(u);
    }
    std::vector<int> b(m + 3);
    for(int i = 1;i <= m;i++){
        std::cin>>b[i];
    }
    auto prdfs = [&](auto &&prdfs, int x, int fa) -> void{
		
		f[x][0] = fa;
		dep[x] = dep[fa] + 1;
		
		for(int i = 1;i < 31;i ++){
			f[x][i] = f[f[x][i - 1]][i - 1];
		}
		
		for(auto i : e[x]) {
			if(i == fa)continue;
			prdfs(prdfs, i, x);
		}
	};
	
	auto lca = [&](int u, int v) -> int{
		
		if(dep[u] < dep[v]) std::swap(u,v);
		int dis = dep[u] - dep[v];
		
		int jp = 0;
		while(dis && jp <= 30){
			if(dis % 2)u = f[u][jp];
			dis >>= 1, jp++;
		}
		//std::cout<<dep[u]<<" "<<dep[v]<<" ";
		
		if(u == v) return u;
		
		for(int i = 30;i >= 0 && u != v;i--){
			if(f[u][i] != f[v][i]) u = f[u][i], v = f[v][i];
		}
		
		return f[u][0];
	};

    auto dis = [&](int u, int v) -> int{
        return dep[u] + dep[v] - 2 * dep[lca(u,v)];
    };
    prdfs(prdfs,b[1],0);
    //std::cout<<q<<" ";
    int ans = m;
    std::vector<int> tag(m + 3);
    for(int i = 2;i < m;i++){
        if(dis(b[i],b[i + 1]) + dis(b[i - 1],b[i]) == dis(b[i - 1],b[i + 1])){
            tag[i] = 1;
            ans--;
        }
        
    }
    //for(int i = 1;i <= m;i++){
        //std::cout<<tag[i]<<" ";
    //}
    for(int j = 1;j <= q;j++){
        
        int i,x;std::cin>>i>>x;
        if(tag[i])ans++,tag[i] = 0;
        if(i + 2 <= m)if(tag[i + 1])ans++,tag[i + 1] = 0;
        //if(tag[i - 1])ans++,tag[i - 1] = 0;
        b[i] = x;
        if(dis(b[i],b[i + 1]) + dis(b[i - 1],b[i]) == dis(b[i - 1],b[i + 1])){
            tag[i] = 1;
            ans--;
        }
        if(i + 2 <= m)
        if(dis(b[i],b[i + 1]) + dis(b[i + 1],b[i + 2]) == dis(b[i + 2],b[i])){
            tag[i + 1] = 1;
            ans--;
        }
        std::cout<<ans<<'\n';       
    }

}

signed main() {
    //io::init();
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    int t = 1;
    // std::cin >> t;
    //    init();
    while (t--) {
        solve();
        // std::cout.flush();
    }
    // std::cout<<5 * inv(2ll) % mod<<'\n';
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3500kb

input:

5 5 3
2 1
3 2
1 4
5 1
1 5 4 2 3
1 3
5 3
3 3

output:

4
4
5

result:

ok 3 number(s): "4 4 5"

Test #2:

score: -100
Wrong Answer
time: 1ms
memory: 3764kb

input:

30 200 200
10 24
10 13
10 26
13 29
27 26
17 24
27 21
17 15
13 5
13 30
27 3
18 21
9 21
2 24
10 4
11 5
2 8
10 23
1 18
21 25
4 20
12 23
22 27
28 27
18 7
13 6
14 30
10 19
16 21
14 29 25 30 1 17 22 21 11 19 21 30 13 1 22 10 14 7 29 7 15 21 25 29 25 7 29 7 1 23 3 17 2 7 4 27 18 26 3 6 5 3 16 26 20 19 16 2...

output:

174
174
174
174
174
173
173
173
173
173
174
174
174
174
174
174
173
173
173
173
172
171
171
171
171
171
171
172
172
172
172
171
171
172
172
172
172
172
172
172
172
172
172
172
172
172
171
171
172
171
171
171
172
172
172
172
172
172
172
172
172
173
172
171
171
171
171
172
171
172
172
172
172
172
171
...

result:

wrong answer 2nd numbers differ - expected: '175', found: '174'