QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#110959#6559. A Tree and Two EdgesUSP_USP_USP#TL 3ms5832kbC++203.5kb2023-06-05 01:20:342023-06-05 01:20:39

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-06-05 01:20:39]
  • 评测
  • 测评结果:TL
  • 用时:3ms
  • 内存:5832kb
  • [2023-06-05 01:20:34]
  • 提交

answer

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

using ll = long long;
#define int ll
#define all(v) (v).begin(), (v).end()
#define pb push_back

void dbg_out() { cerr << endl; }
template<typename H, typename... T> 
void dbg_out(H h, T... t) { cerr << ' ' << h; dbg_out(t...); }
#define dbg(...) { cerr << #__VA_ARGS__ << ':'; dbg_out(__VA_ARGS__); cout << endl; }

struct Seg{
	vector<int> lazy;
	vector<int> seg;
	int n;
	void build(int _n){
		n = _n;
		lazy = vector<int> (4*(n+5));
		seg = vector<int> (4*(n+5));
	}
	void propaga(int r, int i, int j){
		seg[r] += lazy[r];
		if(i != j){
			lazy[2*r] += lazy[r];
			lazy[2*r+1] += lazy[r];
		}
		lazy[r] = 0;
	}

	void update(int r, int i, int j, int a, int b,int v){
		propaga(r,i,j);
		if(i > b || a > j) return;
		if(a <= i && j <= b){
			lazy[r] += v;
			propaga(r,i,j);
			return;
		}
		int m = (i+j)/2;
		update(2*r,i,m,a,b,v);
		update(2*r+1,m+1,j,a,b,v);
		seg[r] = max(seg[2*r], seg[2*r+1]);
	}
	int query(int r, int i, int j,int a, int b){
		propaga(r,i,j);
		if(i > b || a > j) return 0;
		if(a <= i && j <= b) return seg[r];
		int m = (i+j)/2;
		int L = query(2*r,i,m,a,b);
		int R = query(2*r+1,m+1,j,a,b);
		return max(L,R);
	}
	int query(int a, int b){
		return query(1,0,n-1,a,b);
	}
	void update(int a, int b,int v){
		update(1,0,n-1,a,b,v);
	}
};
const int MAXN = 1e5+5;
vector<int> g[MAXN];

struct HLD{
	
	Seg seg;
	vector<int> dad,pos,in,out,h;
	int N;
	HLD(int n, vector<int> g[]): dad(n), pos(n), in(n),out(n),h(n){
		N = n;	
		seg.build(n);
		int t = -1;
		function<void(int)> dfs = [&](int u){
			pos[ in[u] = ++t] = u;
			int mx = -1;
			for(int &v : g[u]) if(v != dad[u]){
				dad[v] = u;
				h[v] = g[u][0] == v ? h[u] : v;
				dfs(v);
				if(out[v] - in[v] > mx){
					mx = out[v]-in[v],swap(g[u][0],v);
				}
			}
			out[u] = t;
		};
		dfs(0);
		t = -1;
		dfs(0);
	}
	void update_path(int u, int v, int val){
		while(h[u] != h[v]){
			if(in[h[u]] < in[h[v]]) swap(u,v);
			seg.update(in[h[u]],in[u],val);
			u = dad[h[u]];
		}
		if(in[u] > in[v]) swap(u,v);
		seg.update(in[u],in[v],val);
	}
	int query(){
		return seg.query(0,N-1);
	}
};

int uf[MAXN];

int find(int x){
	return x == uf[x] ? x : uf[x] = find(uf[x]);
}
int uni(int a, int b){
	a = find(a);
	b = find(b);
	if(a == b) return 0;
	uf[a] = b;
	return 1;
}

void solve() {
	int n;
	cin >> n;
	int q;
	cin >> q;
	vector<array<int,2>> sobra;
	for(int i = 0; i < n; i++) uf[i] = i;
	for(int i = 0; i < n+1; i++){
		int a,b;
		cin >> a >> b;
		a--;
		b--;
		if( uni(a,b)){
			g[a].pb(b);
			g[b].pb(a);
		}else{
			sobra.pb({a,b});
		}
	}
	int a = sobra[0][0];
	int b = sobra[0][1];
	int c = sobra[1][0];
	int d = sobra[1][1];
	HLD hld(n,g);
	for(int i = 0; i < q; i++){
		int u,v;
		cin >> u >> v;
		u--;
		v--;
		vector<vector<pair<int,int>>> ops = {
			{ {u,v} }, 
			{ {u,a},{b,v}},
			{ {u,b},{a,v}},
			{ {u,c},{d,v}},
			{ {u,d},{c,v}},
			{ {u,a},{b,c},{d,v}},
			{ {u,b},{a,c},{d,v}},
			{ {u,a},{b,d},{c,v}},
			{ {u,b},{a,d},{c,v}},
			{ {u,c},{d,a},{b,v}},
			{ {u,c},{d,b},{a,v}},
			{ {u,d},{c,a},{b,v}},
			{ {u,d},{c,b},{a,v}},
		};
		int ans = 0;
		int cnt= 0;
		for(auto y : ops){
			for(auto p : y){
				hld.update_path(p.first,p.second,1);
			}
			int ret =  hld.query() <= 1;
			ans += ret;
			for(auto p : y){
				hld.update_path(p.first,p.second,-1);
			}
		}
		cout << ans << endl;
	}

}

signed main() {
	ios::sync_with_stdio(false); cin.tie(0);
	int t= 1;
	while(t--)
		solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 5832kb

input:

4 6
1 2
1 3
1 4
2 3
2 4
1 2
1 3
1 4
2 3
2 4
3 4

output:

3
3
3
3
3
4

result:

ok 6 lines

Test #2:

score: 0
Accepted
time: 3ms
memory: 5764kb

input:

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

output:

2
2
4
1

result:

ok 4 lines

Test #3:

score: -100
Time Limit Exceeded

input:

50000 50000
11561 23122
14261 28523
24407 48814
17947 35894
14803 29607
19557 39115
12415 24830
9583 19167
18397 36794
439 878
18040 36080
17150 34300
7922 15845
18938 37877
18625 37250
6459 12919
9818 19636
3579 7158
21323 42646
23882 47764
13603 27207
8353 16707
15907 31814
20935 41871
11686 23372...

output:

4
3
3
4
4
3
1
3
4
1
3
4
3
4
3
3
1
3
3
3
4
4
4
3
3
3
4
3
3
3
1
3
3
3
3
4
4
4
4
3
4
3
3
3
4
3
4
4
4
4
3
4
4
4
4
3
3
3
4
4
4
4
4
4
3
4
3
4
1
4
1
1
4
3
3
4
3
3
1
4
3
3
4
4
3
3
4
4
4
3
4
3
4
4
4
4
4
3
4
3
3
3
1
3
4
4
3
4
3
4
3
3
4
1
4
3
3
3
4
4
4
4
3
3
3
3
3
4
4
4
4
3
3
4
3
4
4
3
3
4
4
4
1
3
3
3
3
4
4
3
...

result: