Submission #568180

# Submission time Handle Problem Language Result Execution time Memory
568180 2022-05-24T19:26:00 Z MohamedAhmed04 Travelling Salesperson (CCO20_day2problem1) C++14
25 / 25
535 ms 35988 KB
#include <bits/stdc++.h>

using namespace std ;

const int MAX = 2004 ;

int arr[MAX][MAX] ;
int n ;

int prv[MAX] , nxt[MAX] ;

void solve(int st)
{
	for(int i = 1 ; i <= n ; ++i)
		nxt[i] = prv[i] = -1 ;
	int x = -1 , last = st , stcol = -1 ;
	for(int i = 1 ; i <= n ; ++i)
	{
		if(i == st)
			continue ;
		if(stcol == -1)
			nxt[st] = i , prv[i] = st , stcol = arr[st][i] , last = i ;
		else if(x == -1)
		{
			nxt[last] = i , prv[i] = last ;
			if(arr[last][i] != stcol)
				x = last ;
			last = i ;
		}
		else
		{
			int a = prv[x] ;
			if(arr[x][i] == stcol)
			{
				int y = nxt[x] ;
				nxt[x] = i , prv[i] = x ;
				nxt[i] = y , prv[y] = i ;
			}
			else
			{
				nxt[a] = i , prv[i] = a ;
				nxt[i] = x , prv[x] = i ;
				if(a == st)
					stcol = arr[a][i] ;
			}
			int cur = a ;
			x = -1 ;
			for(int k = 0 ; k < 10 ; ++k)
			{
				if(prv[cur] == -1)
				{
					cur = nxt[cur] ;
					continue ;
				}
				if(nxt[cur] == -1)
					break ;
				if(arr[prv[cur]][cur] != arr[cur][nxt[cur]])
				{
					x = cur ;
					break ;
				}
				cur = nxt[cur] ;
			}
		}
	}
	cout<<n<<"\n" ;
	int now = st ;
	for(int i = 1 ; i <= n ; ++i)
	{
		cout<<now<<" " ;
		now = nxt[now] ;
	}
	cout<<"\n" ;
}

int main()
{
	ios_base::sync_with_stdio(0) ;
	cin.tie(0) ;
	cin>>n ;
	for(int i = 1 ; i <= n ; ++i)
	{
		for(int j = 1 ; j < i ; ++j)
		{
			char c ; 
			cin>>c ;
			if(c == 'B')
				arr[i][j] = arr[j][i] = 0 ;
			else
				arr[i][j] = arr[j][i] = 1 ;
		}
	}
	for(int i = 1 ; i <= n ; ++i)
		solve(i) ;
	return 0 ;
}		
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 296 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 328 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 324 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 332 ms 35716 KB Output is correct
17 Correct 349 ms 35820 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 345 ms 35652 KB Output is correct
20 Correct 429 ms 35860 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 335 ms 35744 KB Output is correct
23 Correct 363 ms 35608 KB Output is correct
24 Correct 1 ms 724 KB Output is correct
25 Correct 325 ms 35716 KB Output is correct
26 Correct 344 ms 35788 KB Output is correct
27 Correct 22 ms 4308 KB Output is correct
28 Correct 340 ms 35920 KB Output is correct
29 Correct 346 ms 35788 KB Output is correct
30 Correct 344 ms 35836 KB Output is correct
31 Correct 336 ms 35716 KB Output is correct
32 Correct 358 ms 35608 KB Output is correct
33 Correct 348 ms 35792 KB Output is correct
34 Correct 340 ms 35584 KB Output is correct
35 Correct 367 ms 35832 KB Output is correct
36 Correct 354 ms 35776 KB Output is correct
37 Correct 352 ms 35588 KB Output is correct
38 Correct 337 ms 35676 KB Output is correct
39 Correct 357 ms 35532 KB Output is correct
40 Correct 364 ms 35784 KB Output is correct
41 Correct 351 ms 35652 KB Output is correct
42 Correct 356 ms 35628 KB Output is correct
43 Correct 363 ms 35772 KB Output is correct
44 Correct 353 ms 35644 KB Output is correct
45 Correct 372 ms 35740 KB Output is correct
46 Correct 344 ms 35656 KB Output is correct
47 Correct 351 ms 35816 KB Output is correct
48 Correct 342 ms 35660 KB Output is correct
49 Correct 340 ms 35848 KB Output is correct
50 Correct 355 ms 35636 KB Output is correct
51 Correct 364 ms 35532 KB Output is correct
52 Correct 421 ms 35800 KB Output is correct
53 Correct 376 ms 35652 KB Output is correct
54 Correct 388 ms 35844 KB Output is correct
55 Correct 399 ms 35872 KB Output is correct
56 Correct 454 ms 35932 KB Output is correct
57 Correct 446 ms 35856 KB Output is correct
58 Correct 434 ms 35840 KB Output is correct
59 Correct 433 ms 35824 KB Output is correct
60 Correct 415 ms 35844 KB Output is correct
61 Correct 425 ms 35788 KB Output is correct
62 Correct 416 ms 35940 KB Output is correct
63 Correct 412 ms 35952 KB Output is correct
64 Correct 439 ms 35780 KB Output is correct
65 Correct 437 ms 35988 KB Output is correct
66 Correct 436 ms 35892 KB Output is correct
67 Correct 411 ms 35840 KB Output is correct
68 Correct 422 ms 35772 KB Output is correct
69 Correct 388 ms 35744 KB Output is correct
70 Correct 405 ms 35856 KB Output is correct
71 Correct 448 ms 35824 KB Output is correct
72 Correct 417 ms 35788 KB Output is correct
73 Correct 430 ms 35872 KB Output is correct
74 Correct 473 ms 35772 KB Output is correct
75 Correct 414 ms 35768 KB Output is correct
76 Correct 426 ms 35788 KB Output is correct
77 Correct 443 ms 35912 KB Output is correct
78 Correct 425 ms 35924 KB Output is correct
79 Correct 413 ms 35884 KB Output is correct
80 Correct 431 ms 35840 KB Output is correct
81 Correct 449 ms 35844 KB Output is correct
82 Correct 463 ms 35804 KB Output is correct
83 Correct 505 ms 35812 KB Output is correct
84 Correct 535 ms 35840 KB Output is correct
85 Correct 495 ms 35856 KB Output is correct
86 Correct 527 ms 35800 KB Output is correct
87 Correct 31 ms 4344 KB Output is correct
88 Correct 28 ms 4372 KB Output is correct
89 Correct 479 ms 35832 KB Output is correct
90 Correct 498 ms 35788 KB Output is correct
91 Correct 26 ms 4356 KB Output is correct
92 Correct 29 ms 4292 KB Output is correct
93 Correct 422 ms 35768 KB Output is correct
94 Correct 423 ms 35788 KB Output is correct
95 Correct 33 ms 4328 KB Output is correct
96 Correct 29 ms 4280 KB Output is correct
97 Correct 413 ms 35900 KB Output is correct
98 Correct 426 ms 35836 KB Output is correct