QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#117425#6559. A Tree and Two EdgesUNos_maricones#WA 19ms8580kbC++203.4kb2023-07-01 07:45:412023-07-01 07:45:42

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-01 07:45:42]
  • 评测
  • 测评结果:WA
  • 用时:19ms
  • 内存:8580kb
  • [2023-07-01 07:45:41]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "../debug.h"
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif


int main(){
	ios_base::sync_with_stdio(0); cin.tie(0);
	int n, q;
	cin >> n >> q;

	vector< vector< int > > g( n );
	vector< int > deg( n );
	for( int i = 0; i < n + 1; ++ i )
	{
		int a, b;
		cin >> a >> b;
		--a, --b;
		g[a].push_back( b );
		g[b].push_back( a );

		++deg[a], ++deg[b];
	}

	queue< int > qq;
	for( int i = 0; i < n; ++ i )
		if( deg[i] == 1 )
			qq.push( i );

	vector< int > par( n );
	iota( par.begin(), par.end(), 0 );
	vector< int > op;

	while( !qq.empty() )
	{
		const int x = qq.front();
		qq.pop();

		op.push_back( x );

		for( auto &y : g[x] )
		{
			if( par[y] == y )
			{
				par[x] = y;
				--deg[y];
				if( deg[y] == 1 )
					qq.push( y );
			}
		}
	}

	reverse( op.begin(), op.end() );
	for( auto x : op )
		par[x] = par[ par[x] ];

	vector< vector< int > > g2( n );
	for( int x = 0; x < n; ++ x )
			for( auto y : g[x] )
				if( par[x] == x && par[y] == y )
					g2[x].push_back( y );

	set< int > heads;
	for( int x = 0; x < n; ++ x )
		if( (int) g2[x].size() >= 3 )
			heads.insert( x );

	vector< vector< int > > is_cycle( 3, vector< int > ( n ) );
	vector< bool > vis( n );

	function< int( int, int, int, int ) > dfs = [&]( int x, int par, int idx, int target )
	{
		if( vis[x] ) return 0;
		vis[x] = 1;

		is_cycle[idx][x] = 0;
		if( x == target )
			return 1;

		for( auto y : g2[x] )
			if( y != par && dfs( y, x, idx, target ) )
				is_cycle[idx][x] = 1;
		
		return is_cycle[idx][x];
	};

	bool are_connected = (int) heads.size() == 2;

	if( are_connected )
	{
		const int h1 = *heads.begin();
		const int h2 = *heads.rbegin();

		int idx = 0;
		for( auto y : g2[h1] )
		{
			fill( vis.begin(), vis.end(), 0 );
			if( dfs( y, h1, idx, h2 ) )
				++idx;
		}

		if( idx < 3 )
		{
			are_connected = false;
			fill( is_cycle[0].begin(), is_cycle[0].end(), 0 );
			fill( is_cycle[1].begin(), is_cycle[1].end(), 0 );
		}
	}

	if( !are_connected )
	{
		int idx = 0;
		for( auto h : heads )
		{
			for( auto y : g2[h] )
			{
				if( is_cycle[0][y] || is_cycle[1][y] ) continue;

				fill( vis.begin(), vis.end(), 0 );
				if( dfs( y, h, idx, h ) )
					++idx;
			}
		}
	}

	//check cases
	while( q-- )
	{
		int u, v;
		cin >> u >> v;
		--u, --v;

		if( par[u] == par[v] )//same subtree
		{
			cout << 1 << '\n';
			continue;
		}
		u = par[u], v = par[v];

		if( !are_connected && (int) heads.size() == 2 )
		{
			int w = 1;
			if( is_cycle[0][u] || is_cycle[0][v] ) w *= 2;
			if( is_cycle[1][u] || is_cycle[1][v] ) w *= 2;

			cout << w << '\n';
			continue;
		}

		if( !are_connected )
		{
			if( heads.count( u ) || heads.count( v ) )
				cout << 2 << '\n';
			else
			{
				int w = 1;
				if( is_cycle[0][u] || is_cycle[0][v] ) w *= 2;
				if( is_cycle[1][u] || is_cycle[1][v] ) w *= 2;

				cout << w << '\n';
			}
			continue;
		}

		//special case

		if( heads.count( u ) || heads.count( v ) )
		{
			cout << 3 << '\n';
			continue;
		}

		int ans = 0;
		for( int idx = 0; idx < 3; ++ idx )
				if( is_cycle[idx][u] != is_cycle[idx][v] )
					ans = 1;

		ans += 3;
		cout << ans << '\n';
	}

	return 0;
}

详细

Test #1:

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

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: 3420kb

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: 0
Accepted
time: 17ms
memory: 8552kb

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:

ok 50000 lines

Test #4:

score: 0
Accepted
time: 17ms
memory: 8580kb

input:

50000 50000
1730 3460
17535 35071
14108 28216
20630 41260
2091 4182
8112 16225
21373 42746
6685 13371
21142 42284
12168 24337
22564 45128
16103 32207
9254 18508
21369 42739
1955 3911
13696 27392
3929 7858
1777 3555
23382 46764
830 1660
17722 35444
11495 22991
10184 20369
13697 27395
24728 49456
4037...

output:

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

result:

ok 50000 lines

Test #5:

score: -100
Wrong Answer
time: 19ms
memory: 8504kb

input:

50000 50000
21879 43758
12510 25020
2593 5187
16048 32096
9697 19394
12606 25212
3860 7720
8231 16462
23983 47966
10852 21705
6919 13839
1385 2770
4040 8080
14298 28596
22248 44496
4245 8490
14486 28972
11445 22891
21557 43114
20946 41892
23374 46749
78 157
4617 9234
8198 16396
12228 24456
16125 322...

output:

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

result:

wrong answer 2nd lines differ - expected: '2', found: '3'