QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#414770#6559. A Tree and Two Edgeskaruna#WA 25ms7396kbC++202.1kb2024-05-19 17:11:252024-05-19 17:11:26

Judging History

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

  • [2024-05-19 17:11:26]
  • 评测
  • 测评结果:WA
  • 用时:25ms
  • 内存:7396kb
  • [2024-05-19 17:11:25]
  • 提交

answer

#include <bits/stdc++.h>
#define ff first
#define ss second
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 5e4;


int N, Q;
vector<int> adj[MAXN+10];
vector<vector<int>> V;
vector<pii> E;

int vis[MAXN+10], par[MAXN+10];
void dfs(int now, int bef)
{
	vis[now]=1;
	par[now]=bef;
	for(int nxt : adj[now])
	{
		if(nxt==bef) continue;
		if(vis[nxt]==0) dfs(nxt, now);
		else if(vis[nxt]==1)
		{
			vector<int> VV;
			for(int i=now; i!=nxt; i=par[i]) VV.push_back(i);
			VV.push_back(nxt);
			V.push_back(VV);
		}
	}
	vis[now]=2;
}

bool P[MAXN+10];
int P2[MAXN+10];
void chk(int u, int v)
{
	if(u>v) swap(u, v);
	P[lower_bound(E.begin(), E.end(), pii(u, v))-E.begin()]=1;
}

struct UF
{
	int par[MAXN+10];
	void init()
	{
		for(int i=1; i<=N; i++) par[i]=i;
	}
	int Find(int x)
	{
		if(x==par[x]) return x;
		return par[x]=Find(par[x]);
	}
	void Union(int x, int y)
	{
		x=Find(x);
		y=Find(y);
		par[x]=y;
	}
}uf;

int main() {
	cin.tie(0); ios_base::sync_with_stdio(0);

	scanf("%d%d", &N, &Q);
	for(int i=1; i<=N+1; i++)
	{
		int u, v;
		scanf("%d%d", &u, &v);
		if(u>v) swap(u, v);
		E.push_back({u, v});
		adj[u].push_back(v);
		adj[v].push_back(u);
	}

	uf.init();
	sort(E.begin(), E.end());

	dfs(1, 1);
	//assert(V.size()==2);

	for(int i=0; i+1<V[0].size(); i++) chk(V[0][i], V[0][i+1]);
	chk(V[0][V[0].size()-1], V[0][0]);
	
	for(int i=0; i+1<V[1].size(); i++) chk(V[1][i], V[1][i+1]);
	chk(V[1][V[1].size()-1], V[1][0]);

	for(int i=0; i<E.size(); i++)
	{
		if(P[i]) continue;
		uf.Union(E[i].first, E[i].second);
	}

    for(auto jt : V[0]) P2[uf.Find(jt)]|=1;
	for(auto jt : V[1]) P2[uf.Find(jt)]|=2;

	int cnt=0;
	for(int i=1; i<=N; i++) if(P2[i]==3) cnt++;

	while(Q--)
	{
		int u, v;
		scanf("%d%d", &u, &v);
		if(uf.Find(u)==uf.Find(v)) printf("1\n");
		else if(P2[uf.Find(u)]==3 || P2[uf.Find(v)]==3)
		{
			if(cnt==1) printf("2\n");
			else printf("3\n");
		}
		else if(P2[uf.Find(u)]==P2[uf.Find(v)])
		{
			if(cnt==1) printf("2\n");
			else printf("3\n");
		}
		else printf("4\n");
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 1ms
memory: 3804kb

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
Wrong Answer
time: 25ms
memory: 7396kb

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:

3
3
3
3
3
3
1
3
3
1
3
3
3
3
3
3
1
3
3
3
3
3
3
3
3
3
3
3
3
3
1
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
1
3
1
1
3
3
3
3
3
3
1
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
1
3
3
3
3
3
3
3
3
3
3
1
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
1
3
3
3
3
3
3
3
...

result:

wrong answer 1st lines differ - expected: '4', found: '3'