Submission #351250

# Submission time Handle Problem Language Result Execution time Memory
351250 2021-01-19T17:31:07 Z CaroLinda Simurgh (IOI17_simurgh) C++14
100 / 100
226 ms 7404 KB
#include "simurgh.h"
#include <bits/stdc++.h>

#define debug 
#define ll long long
#define all(x) x.begin(),x.end()
#define sz(x) (int)(x.size() )

const int MAXN = 505 ;
const int MAXM = MAXN*MAXN ;

using namespace std ;

int n , m , treeAns ;
int U[MAXM] , V[MAXM] , pai[MAXN] ;
int cumulativeFoundOnes[MAXN] , lvl[MAXN] , edgeFather[MAXN] ;
int edgeStatus[MAXM] ;
vector< pair<int,int> > adj[MAXN] ;
vector< pair<int,int> > tree[MAXN] ;
vector<int> treeIds ;
bool vis[MAXN] , isTree[MAXM] ;

int find(int x)
{
	if( x == pai[x] ) return x ;
	return pai[x] = find(pai[x]) ;
}

bool join(int x, int y )
{
	x = find(x) ;
	y = find(y) ;

	if(x == y ) return false ;

	if(rand() % 2 ) swap(x,y) ;

	pai[x] = y ;

	return true ;

}

void cleanTime() { for(int i = 0 ; i< n ; i++ ) pai[i] = i ; }

int getOther(int edgeId, int knownEnd )
{
	if( knownEnd == U[edgeId] ) return V[edgeId] ;
	return U[edgeId] ;
}

void findTree(int x)
{
	vis[x] = true ;

	for(auto e : adj[x] )
	{
		int j = e.first ;
		int id = e.second ;

		if( vis[j] ) continue ;

		edgeFather[j] = id ;
		isTree[id] = true ;
		treeIds.push_back(id) ;
		lvl[j] = lvl[x] + 1 ;

		tree[x].push_back(make_pair(j, id) ) ;

		findTree(j) ;

	}
}

void recalcCumulative(int x)
{
	
	for(auto e : tree[x] )
	{
		int j = e.first ;
		int id = e.second ;

		cumulativeFoundOnes[j] += cumulativeFoundOnes[x] ;
		if(edgeStatus[id] != -1 ) cumulativeFoundOnes[j]++ ;

		recalcCumulative(j) ;
	}

}

void bb(vector<int> v, int commonRoads )
{

	if( commonRoads == 0 || sz(v) == 0 ) 
	{
		for(auto e : v ) edgeStatus[e] = 0 ;
		return ;
	}

	if(sz(v) == commonRoads )
	{
		for(auto e : v ) edgeStatus[e] = 1 ;
		return ;
	}

	vector<int> l , r ;

	for(int i= 0 ; i < sz(v)/2 ; i++ ) l.push_back(v[i] ) ;
	for(int i = sz(v)/2 ; i < sz(v) ; i++ ) r.push_back(v[i] ) ;

	cleanTime() ;

	vector<int> vec = l ;

	int c = 0 ;

	for(auto e : l ) join(U[e] , V[e] ) ;
	for(auto e : treeIds ) 
		if(join(U[e] , V[e] ) ) 
		{
			vec.push_back(e) ;
			c -= edgeStatus[e] ;
		}

	c += count_common_roads(vec) ;

	bb(l, c) ;
	bb(r, commonRoads - c ) ;

}

vector<int> find_roads(int N, vector<int> u, vector<int> v) 
{
	n = N ;
	m = sz(u) ;

	for(int i = 0 ; i < m ; i++ )
	{
		adj[ u[i] ].push_back(make_pair(v[i],i) ) ;
		adj[ v[i] ].push_back( make_pair(u[i], i) ) ;

		U[i] = u[i] ;
		V[i] = v[i] ;

		edgeStatus[i] = -1 ;

	}


	edgeFather[0] = -1 ;
	findTree(0 ) ;
	
	treeAns = count_common_roads( treeIds ) ;

	for(auto e : treeIds ) debug("%d (which is %d %d) is in tree\n", e, U[e], V[e] ) ;
	debug("\n") ;

	for(int i = 0 ; i < m ; i++ )
	{
		if(isTree[i] ) continue ;

		int up = U[i] , down = V[i] ;

		if(lvl[up] > lvl[down] ) swap(up,down ) ;

		if( cumulativeFoundOnes[down] - cumulativeFoundOnes[up] == lvl[down]-lvl[up] ) continue ;

		vector<int> notFound ;
		vector<int> alreadyFound ;

		int x = down ;

		while( x != up )
		{

			if( edgeStatus[ edgeFather[x] ] == -1 ) notFound.push_back( edgeFather[x] ) ;
			else alreadyFound.push_back( edgeFather[x] ) ;

			x = getOther(edgeFather[x] , x ) ;

		}


		if( sz(alreadyFound) != 0 )
		{
			vector<int> r(1,i) ;

			for(int e : treeIds )
				if( e != alreadyFound[0] ) r.push_back( e ) ;

		  	if( count_common_roads(r) == treeAns-edgeStatus[ alreadyFound[0] ] )
		  		edgeStatus[i] = 0 ;
		 	else edgeStatus[i] = 1 ; 

		 	for(auto e : notFound )
		 	{
				r.clear() ;

				for(auto idx : treeIds )
					if(idx != e ) r.push_back(idx) ;

				r.push_back( i ) ;

				if( treeAns == count_common_roads(r) - edgeStatus[i] )
					edgeStatus[e] = 0 ;
				else edgeStatus[e] = 1 ;

		 	}

		}
		else 
		{
			//If I'm golden, as we can't all be golden, then I will increase at least once
			//If I'm not golden, I can never increase

			vector<int> ans ;

			edgeStatus[i] = 0 ;

			for(auto e : notFound ) 
			{
				vector<int> r(1,i) ;

				for(auto idx : treeIds )
					if(idx != e )	r.push_back(idx) ;

				ans.push_back(count_common_roads(r)) ;

				if( ans.back() == treeAns+1 ) edgeStatus[i] = 1 ;
			}

			for(int j = 0 ; j < sz(notFound) ; j++ )
			{
				if( treeAns == ans[j] - edgeStatus[i] )
					edgeStatus[ notFound[j] ] = 0 ;
				else edgeStatus[ notFound[j] ] = 1 ; 			
			}


		}

		for(int i = 0 ; i< n ; i++ ) cumulativeFoundOnes[i] = 0 ;

		recalcCumulative(0) ;

	}

	for(int e : treeIds )
		if(edgeStatus[e] == -1 ) edgeStatus[e] = 1 ; //I know for a fact they are golden (they are bridges)

	for(auto e : treeIds ) debug("edge %d (%d and %d) is %d\n", e, U[e] , V[e] , edgeStatus[e] ) ;
	for(int i = 0 ; i < m; i++ ) debug("%d ", edgeStatus[i] ) ;
	debug("\n") ;

  	for(int i = 0 ; i < n ; i++ )
  	{
  		vector<int> r ;

  		cleanTime() ;	

		for(auto e : adj[i] ) 
			if( edgeStatus[e.second] == -1 ) 
			{
				r.push_back(e.second) ;
				join(i , e.first ) ;
			}

		if(sz(r) == 0 ) continue ;

		int tot = 0 ;

		for(auto e : treeIds)
			if( join(U[e] , V[e] ) )
			{
				tot -= edgeStatus[e] ;
				r.push_back(e) ;
			}

		tot += count_common_roads(r) ;
	
		while( isTree[ r.back() ] ) r.pop_back() ;

		bb(r, tot ) ;

  	}

  	vector<int> goldenSet ;
  	for(int i = 0 ; i< m ; i++ )
  		if(edgeStatus[i] == 1 ) goldenSet.push_back(i) ;

  	return goldenSet ;

}

Compilation message

simurgh.cpp: In function 'std::vector<int> find_roads(int, std::vector<int>, std::vector<int>)':
simurgh.cpp:155:31: warning: left operand of comma operator has no effect [-Wunused-value]
  155 |  for(auto e : treeIds ) debug("%d (which is %d %d) is in tree\n", e, U[e], V[e] ) ;
      |                               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
simurgh.cpp:155:73: warning: right operand of comma operator has no effect [-Wunused-value]
  155 |  for(auto e : treeIds ) debug("%d (which is %d %d) is in tree\n", e, U[e], V[e] ) ;
      |                                                                         ^
simurgh.cpp:155:73: warning: right operand of comma operator has no effect [-Wunused-value]
  155 |  for(auto e : treeIds ) debug("%d (which is %d %d) is in tree\n", e, U[e], V[e] ) ;
      |                                                                      ~~~^
simurgh.cpp:156:8: warning: statement has no effect [-Wunused-value]
  156 |  debug("\n") ;
      |       ~^~~~~
simurgh.cpp:251:31: warning: left operand of comma operator has no effect [-Wunused-value]
  251 |  for(auto e : treeIds ) debug("edge %d (%d and %d) is %d\n", e, U[e] , V[e] , edgeStatus[e] ) ;
      |                               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
simurgh.cpp:251:68: warning: right operand of comma operator has no effect [-Wunused-value]
  251 |  for(auto e : treeIds ) debug("edge %d (%d and %d) is %d\n", e, U[e] , V[e] , edgeStatus[e] ) ;
      |                                                                    ^
simurgh.cpp:251:68: warning: right operand of comma operator has no effect [-Wunused-value]
  251 |  for(auto e : treeIds ) debug("edge %d (%d and %d) is %d\n", e, U[e] , V[e] , edgeStatus[e] ) ;
      |                                                                 ~~~^
simurgh.cpp:251:75: warning: right operand of comma operator has no effect [-Wunused-value]
  251 |  for(auto e : treeIds ) debug("edge %d (%d and %d) is %d\n", e, U[e] , V[e] , edgeStatus[e] ) ;
      |                                                                        ~~~^
simurgh.cpp:252:37: warning: left operand of comma operator has no effect [-Wunused-value]
  252 |  for(int i = 0 ; i < m; i++ ) debug("%d ", edgeStatus[i] ) ;
      |                                     ^~~~~
simurgh.cpp:253:8: warning: statement has no effect [-Wunused-value]
  253 |  debug("\n") ;
      |       ~^~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB correct
2 Correct 1 ms 364 KB correct
3 Correct 1 ms 364 KB correct
4 Correct 1 ms 364 KB correct
5 Correct 1 ms 364 KB correct
6 Correct 1 ms 364 KB correct
7 Correct 1 ms 364 KB correct
8 Correct 1 ms 364 KB correct
9 Correct 1 ms 364 KB correct
10 Correct 1 ms 364 KB correct
11 Correct 1 ms 364 KB correct
12 Correct 1 ms 364 KB correct
13 Correct 1 ms 364 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB correct
2 Correct 1 ms 364 KB correct
3 Correct 1 ms 364 KB correct
4 Correct 1 ms 364 KB correct
5 Correct 1 ms 364 KB correct
6 Correct 1 ms 364 KB correct
7 Correct 1 ms 364 KB correct
8 Correct 1 ms 364 KB correct
9 Correct 1 ms 364 KB correct
10 Correct 1 ms 364 KB correct
11 Correct 1 ms 364 KB correct
12 Correct 1 ms 364 KB correct
13 Correct 1 ms 364 KB correct
14 Correct 2 ms 492 KB correct
15 Correct 1 ms 512 KB correct
16 Correct 2 ms 620 KB correct
17 Correct 2 ms 492 KB correct
18 Correct 1 ms 364 KB correct
19 Correct 3 ms 492 KB correct
20 Correct 2 ms 364 KB correct
21 Correct 2 ms 492 KB correct
22 Correct 1 ms 492 KB correct
23 Correct 1 ms 364 KB correct
24 Correct 1 ms 364 KB correct
25 Correct 1 ms 364 KB correct
26 Correct 1 ms 364 KB correct
27 Correct 1 ms 364 KB correct
28 Correct 1 ms 364 KB correct
29 Correct 1 ms 364 KB correct
30 Correct 1 ms 364 KB correct
31 Correct 1 ms 364 KB correct
32 Correct 1 ms 364 KB correct
33 Correct 1 ms 364 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB correct
2 Correct 1 ms 364 KB correct
3 Correct 1 ms 364 KB correct
4 Correct 1 ms 364 KB correct
5 Correct 1 ms 364 KB correct
6 Correct 1 ms 364 KB correct
7 Correct 1 ms 364 KB correct
8 Correct 1 ms 364 KB correct
9 Correct 1 ms 364 KB correct
10 Correct 1 ms 364 KB correct
11 Correct 1 ms 364 KB correct
12 Correct 1 ms 364 KB correct
13 Correct 1 ms 364 KB correct
14 Correct 2 ms 492 KB correct
15 Correct 1 ms 512 KB correct
16 Correct 2 ms 620 KB correct
17 Correct 2 ms 492 KB correct
18 Correct 1 ms 364 KB correct
19 Correct 3 ms 492 KB correct
20 Correct 2 ms 364 KB correct
21 Correct 2 ms 492 KB correct
22 Correct 1 ms 492 KB correct
23 Correct 1 ms 364 KB correct
24 Correct 1 ms 364 KB correct
25 Correct 1 ms 364 KB correct
26 Correct 1 ms 364 KB correct
27 Correct 1 ms 364 KB correct
28 Correct 1 ms 364 KB correct
29 Correct 1 ms 364 KB correct
30 Correct 1 ms 364 KB correct
31 Correct 1 ms 364 KB correct
32 Correct 1 ms 364 KB correct
33 Correct 1 ms 364 KB correct
34 Correct 42 ms 2028 KB correct
35 Correct 40 ms 1900 KB correct
36 Correct 34 ms 1644 KB correct
37 Correct 10 ms 492 KB correct
38 Correct 41 ms 2028 KB correct
39 Correct 34 ms 1920 KB correct
40 Correct 35 ms 1644 KB correct
41 Correct 44 ms 2156 KB correct
42 Correct 42 ms 2028 KB correct
43 Correct 17 ms 1388 KB correct
44 Correct 15 ms 1132 KB correct
45 Correct 17 ms 1260 KB correct
46 Correct 15 ms 1004 KB correct
47 Correct 11 ms 748 KB correct
48 Correct 4 ms 364 KB correct
49 Correct 7 ms 492 KB correct
50 Correct 11 ms 748 KB correct
51 Correct 17 ms 1260 KB correct
52 Correct 16 ms 1132 KB correct
53 Correct 17 ms 1004 KB correct
54 Correct 18 ms 1388 KB correct
55 Correct 16 ms 1260 KB correct
56 Correct 19 ms 1260 KB correct
57 Correct 15 ms 1260 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB correct
2 Correct 1 ms 364 KB correct
3 Correct 121 ms 5120 KB correct
4 Correct 202 ms 7276 KB correct
5 Correct 217 ms 7276 KB correct
6 Correct 180 ms 7020 KB correct
7 Correct 134 ms 7148 KB correct
8 Correct 143 ms 7148 KB correct
9 Correct 196 ms 7148 KB correct
10 Correct 208 ms 7404 KB correct
11 Correct 208 ms 7276 KB correct
12 Correct 208 ms 7020 KB correct
13 Correct 1 ms 364 KB correct
14 Correct 169 ms 7020 KB correct
15 Correct 226 ms 7148 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB correct
2 Correct 1 ms 364 KB correct
3 Correct 1 ms 364 KB correct
4 Correct 1 ms 364 KB correct
5 Correct 1 ms 364 KB correct
6 Correct 1 ms 364 KB correct
7 Correct 1 ms 364 KB correct
8 Correct 1 ms 364 KB correct
9 Correct 1 ms 364 KB correct
10 Correct 1 ms 364 KB correct
11 Correct 1 ms 364 KB correct
12 Correct 1 ms 364 KB correct
13 Correct 1 ms 364 KB correct
14 Correct 2 ms 492 KB correct
15 Correct 1 ms 512 KB correct
16 Correct 2 ms 620 KB correct
17 Correct 2 ms 492 KB correct
18 Correct 1 ms 364 KB correct
19 Correct 3 ms 492 KB correct
20 Correct 2 ms 364 KB correct
21 Correct 2 ms 492 KB correct
22 Correct 1 ms 492 KB correct
23 Correct 1 ms 364 KB correct
24 Correct 1 ms 364 KB correct
25 Correct 1 ms 364 KB correct
26 Correct 1 ms 364 KB correct
27 Correct 1 ms 364 KB correct
28 Correct 1 ms 364 KB correct
29 Correct 1 ms 364 KB correct
30 Correct 1 ms 364 KB correct
31 Correct 1 ms 364 KB correct
32 Correct 1 ms 364 KB correct
33 Correct 1 ms 364 KB correct
34 Correct 42 ms 2028 KB correct
35 Correct 40 ms 1900 KB correct
36 Correct 34 ms 1644 KB correct
37 Correct 10 ms 492 KB correct
38 Correct 41 ms 2028 KB correct
39 Correct 34 ms 1920 KB correct
40 Correct 35 ms 1644 KB correct
41 Correct 44 ms 2156 KB correct
42 Correct 42 ms 2028 KB correct
43 Correct 17 ms 1388 KB correct
44 Correct 15 ms 1132 KB correct
45 Correct 17 ms 1260 KB correct
46 Correct 15 ms 1004 KB correct
47 Correct 11 ms 748 KB correct
48 Correct 4 ms 364 KB correct
49 Correct 7 ms 492 KB correct
50 Correct 11 ms 748 KB correct
51 Correct 17 ms 1260 KB correct
52 Correct 16 ms 1132 KB correct
53 Correct 17 ms 1004 KB correct
54 Correct 18 ms 1388 KB correct
55 Correct 16 ms 1260 KB correct
56 Correct 19 ms 1260 KB correct
57 Correct 15 ms 1260 KB correct
58 Correct 1 ms 364 KB correct
59 Correct 1 ms 364 KB correct
60 Correct 121 ms 5120 KB correct
61 Correct 202 ms 7276 KB correct
62 Correct 217 ms 7276 KB correct
63 Correct 180 ms 7020 KB correct
64 Correct 134 ms 7148 KB correct
65 Correct 143 ms 7148 KB correct
66 Correct 196 ms 7148 KB correct
67 Correct 208 ms 7404 KB correct
68 Correct 208 ms 7276 KB correct
69 Correct 208 ms 7020 KB correct
70 Correct 1 ms 364 KB correct
71 Correct 169 ms 7020 KB correct
72 Correct 226 ms 7148 KB correct
73 Correct 1 ms 492 KB correct
74 Correct 203 ms 7032 KB correct
75 Correct 210 ms 7020 KB correct
76 Correct 98 ms 3052 KB correct
77 Correct 204 ms 7148 KB correct
78 Correct 204 ms 7148 KB correct
79 Correct 207 ms 7276 KB correct
80 Correct 206 ms 7020 KB correct
81 Correct 123 ms 6276 KB correct
82 Correct 215 ms 6892 KB correct
83 Correct 145 ms 4332 KB correct
84 Correct 82 ms 4844 KB correct
85 Correct 84 ms 4588 KB correct
86 Correct 67 ms 3052 KB correct
87 Correct 62 ms 2540 KB correct
88 Correct 51 ms 1900 KB correct
89 Correct 53 ms 2032 KB correct
90 Correct 49 ms 1792 KB correct
91 Correct 26 ms 620 KB correct
92 Correct 14 ms 492 KB correct
93 Correct 82 ms 4716 KB correct
94 Correct 63 ms 3052 KB correct
95 Correct 69 ms 3052 KB correct
96 Correct 51 ms 1772 KB correct
97 Correct 54 ms 1900 KB correct
98 Correct 57 ms 2540 KB correct
99 Correct 51 ms 1900 KB correct
100 Correct 33 ms 748 KB correct
101 Correct 15 ms 492 KB correct
102 Correct 76 ms 4012 KB correct
103 Correct 73 ms 3820 KB correct
104 Correct 73 ms 3948 KB correct
105 Correct 70 ms 3820 KB correct