답안 #987452

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
987452 2024-05-22T19:22:37 Z Pyqe 가장 긴 여행 (IOI23_longesttrip) C++17
60 / 100
850 ms 2756 KB
#include <bits/stdc++.h>
#include "longesttrip.h"

using namespace std;

long long n,nn[2],dh[100069],pr[100069],ex[2][100069],tmp[100069],sq[100069],zs;
bitset<1069> am[1069];
bitset<100069> vtd;

inline bool qr(vector<long long> v,vector<long long> v2)
{
	long long i,j,sz,sz2;
	
	sz=v.size();
	sz2=v2.size();
	for(i=0;i<sz;i++)
	{
		for(j=0;j<sz2;j++)
		{
			if(am[v[i]][v2[j]])
			{
				return 1;
			}
		}
	}
	return 0;
}

void dfs(long long x)
{
	long long i;
	
	vtd[x]=1;
	for(i=1;i<=n;i++)
	{
		if(!vtd[i]&&!qr({x},{i}))
		{
			dh[i]=dh[x]+1;
			pr[i]=x;
			dfs(i);
		}
	}
}

bool cmdh(long long x,long long y)
{
	return dh[x]<dh[y];
}

vector<int> longest_trip(int on,int d)
{
	long long i,j,ii,jj,k,l,sz,e,lh,rh,md,zz;
	vector<long long> cv,cv2;
	vector<int> ret;
	
	n=on;
	for(i=1;i<=n;i++)
	{
		for(j=i+1;j<=n;j++)
		{
			k=are_connected({(int)i-1},{(int)j-1});
			am[i][j]=k;
			am[j][i]=k;
		}
	}
	vtd.reset();
	for(ii=0;ii<2;ii++)
	{
		nn[ii]=0;
	}
	for(i=1;i<=n;i++)
	{
		if(!vtd[i])
		{
			dh[i]=0;
			dfs(i);
		}
		e=dh[i]%2;
		nn[e]++;
		ex[e][nn[e]]=i;
	}
	for(ii=0;ii<2;ii++)
	{
		sort(ex[ii]+1,ex[ii]+nn[ii]+1,cmdh);
		sz=0;
		l=-1;
		vtd.reset();
		for(i=nn[ii];i;i--)
		{
			k=ex[ii][i];
			if(l==-1)
			{
				l=k;
			}
			else if(dh[k]!=dh[l])
			{
				l=pr[pr[l]];
				sz++;
				tmp[sz]=l;
				vtd[l]=1;
			}
			if(!vtd[k])
			{
				sz++;
				tmp[sz]=k;
				l=k;
			}
		}
		for(i=1;i<=sz;i++)
		{
			ex[ii][i]=tmp[i];
		}
	}
	zs=0;
	for(i=1;i<=nn[0];i++)
	{
		cv.push_back(ex[0][i]);
	}
	for(i=1;i<=nn[1];i++)
	{
		cv2.push_back(ex[1][i]);
	}
	if(!nn[1]||!qr(cv,cv2))
	{
		e=nn[1]>nn[0];
		for(i=1;i<=nn[e];i++)
		{
			zs++;
			sq[zs]=ex[e][i];
		}
	}
	else
	{
		for(ii=0;ii<2;ii++)
		{
			k=ex[0][1];
			for(jj=0;jj<2;jj++)
			{
				l=ex[1][1];
				if(!zs&&qr({k},{l}))
				{
					for(i=nn[0];i;i--)
					{
						zs++;
						sq[zs]=ex[0][i];
					}
					for(i=1;i<=nn[1];i++)
					{
						zs++;
						sq[zs]=ex[1][i];
					}
				}
				reverse(ex[1]+1,ex[1]+nn[1]+1);
			}
			reverse(ex[0]+1,ex[0]+nn[0]+1);
		}
		if(!zs)
		{
			cv2.clear();
			for(i=1;i<=nn[1];i++)
			{
				cv2.push_back(ex[1][i]);
			}
			for(ii=0;ii<2;ii++)
			{
				for(lh=1,rh=nn[ii];lh<=rh;)
				{
					md=(lh+rh)/2;
					cv.clear();
					for(i=1;i<=md;i++)
					{
						cv.push_back(ex[ii][i]);
					}
					if(qr(cv,cv2))
					{
						zz=md;
						rh=md-1;
					}
					else
					{
						lh=md+1;
					}
				}
				k=zz;
				swap(k,l);
				cv2.clear();
				for(i=1;i<=zz;i++)
				{
					cv2.push_back(ex[ii][i]);
				}
			}
			for(i=k+1;i<=nn[0];i++)
			{
				zs++;
				sq[zs]=ex[0][i];
			}
			for(i=1;i<=k;i++)
			{
				zs++;
				sq[zs]=ex[0][i];
			}
			for(i=l;i<=nn[1];i++)
			{
				zs++;
				sq[zs]=ex[1][i];
			}
			for(i=1;i<l;i++)
			{
				zs++;
				sq[zs]=ex[1][i];
			}
		}
	}
	for(i=1;i<=zs;i++)
	{
		ret.push_back(sq[i]-1);
	}
	return ret;
}

Compilation message

longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:210:11: warning: 'zz' may be used uninitialized in this function [-Wmaybe-uninitialized]
  210 |     sq[zs]=ex[1][i];
      |     ~~~~~~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 169 ms 456 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 344 KB Output is correct
2 Correct 27 ms 344 KB Output is correct
3 Correct 117 ms 456 KB Output is correct
4 Correct 316 ms 452 KB Output is correct
5 Correct 721 ms 600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 344 KB Output is correct
2 Correct 21 ms 500 KB Output is correct
3 Correct 120 ms 600 KB Output is correct
4 Correct 371 ms 464 KB Output is correct
5 Correct 747 ms 456 KB Output is correct
6 Correct 7 ms 344 KB Output is correct
7 Correct 26 ms 344 KB Output is correct
8 Correct 116 ms 600 KB Output is correct
9 Correct 294 ms 440 KB Output is correct
10 Correct 714 ms 452 KB Output is correct
11 Correct 739 ms 452 KB Output is correct
12 Correct 738 ms 852 KB Output is correct
13 Correct 717 ms 456 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 344 KB Output is correct
2 Correct 23 ms 344 KB Output is correct
3 Correct 104 ms 452 KB Output is correct
4 Correct 339 ms 856 KB Output is correct
5 Correct 717 ms 464 KB Output is correct
6 Correct 8 ms 600 KB Output is correct
7 Correct 25 ms 344 KB Output is correct
8 Correct 126 ms 600 KB Output is correct
9 Correct 280 ms 2756 KB Output is correct
10 Correct 746 ms 600 KB Output is correct
11 Correct 675 ms 452 KB Output is correct
12 Correct 719 ms 452 KB Output is correct
13 Correct 725 ms 452 KB Output is correct
14 Correct 7 ms 344 KB Output is correct
15 Correct 12 ms 344 KB Output is correct
16 Correct 32 ms 344 KB Output is correct
17 Correct 76 ms 600 KB Output is correct
18 Correct 113 ms 2648 KB Output is correct
19 Correct 285 ms 456 KB Output is correct
20 Correct 261 ms 592 KB Output is correct
21 Correct 710 ms 2500 KB Output is correct
22 Correct 714 ms 592 KB Output is correct
23 Correct 749 ms 856 KB Output is correct
24 Correct 737 ms 460 KB Output is correct
25 Correct 9 ms 344 KB Output is correct
26 Correct 8 ms 596 KB Output is correct
27 Correct 30 ms 344 KB Output is correct
28 Correct 28 ms 340 KB Output is correct
29 Correct 29 ms 344 KB Output is correct
30 Correct 158 ms 700 KB Output is correct
31 Correct 156 ms 700 KB Output is correct
32 Correct 180 ms 452 KB Output is correct
33 Correct 274 ms 2480 KB Output is correct
34 Correct 258 ms 436 KB Output is correct
35 Correct 249 ms 344 KB Output is correct
36 Correct 713 ms 2656 KB Output is correct
37 Correct 702 ms 456 KB Output is correct
38 Correct 725 ms 600 KB Output is correct
39 Correct 708 ms 2504 KB Output is correct
40 Correct 713 ms 452 KB Output is correct
41 Correct 753 ms 2508 KB Output is correct
42 Correct 751 ms 460 KB Output is correct
43 Correct 774 ms 456 KB Output is correct
44 Correct 764 ms 2504 KB Output is correct
45 Correct 8 ms 344 KB Output is correct
46 Correct 10 ms 344 KB Output is correct
47 Correct 19 ms 344 KB Output is correct
48 Correct 24 ms 344 KB Output is correct
49 Correct 26 ms 500 KB Output is correct
50 Correct 164 ms 2748 KB Output is correct
51 Correct 152 ms 700 KB Output is correct
52 Correct 145 ms 2480 KB Output is correct
53 Correct 253 ms 344 KB Output is correct
54 Correct 237 ms 500 KB Output is correct
55 Correct 241 ms 592 KB Output is correct
56 Correct 785 ms 2500 KB Output is correct
57 Correct 765 ms 520 KB Output is correct
58 Correct 738 ms 528 KB Output is correct
59 Correct 766 ms 536 KB Output is correct
60 Correct 598 ms 2568 KB Output is correct
61 Correct 742 ms 600 KB Output is correct
62 Correct 766 ms 548 KB Output is correct
63 Correct 748 ms 2580 KB Output is correct
64 Correct 704 ms 556 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 344 KB Output is correct
2 Correct 20 ms 344 KB Output is correct
3 Partially correct 114 ms 600 KB Output is partially correct
4 Partially correct 330 ms 440 KB Output is partially correct
5 Partially correct 850 ms 2504 KB Output is partially correct
6 Correct 7 ms 344 KB Output is correct
7 Correct 21 ms 340 KB Output is correct
8 Partially correct 126 ms 604 KB Output is partially correct
9 Partially correct 271 ms 432 KB Output is partially correct
10 Partially correct 731 ms 456 KB Output is partially correct
11 Partially correct 702 ms 456 KB Output is partially correct
12 Partially correct 721 ms 456 KB Output is partially correct
13 Partially correct 748 ms 452 KB Output is partially correct
14 Correct 7 ms 344 KB Output is correct
15 Correct 12 ms 344 KB Output is correct
16 Correct 49 ms 344 KB Output is correct
17 Partially correct 76 ms 600 KB Output is partially correct
18 Partially correct 122 ms 600 KB Output is partially correct
19 Partially correct 257 ms 436 KB Output is partially correct
20 Partially correct 263 ms 432 KB Output is partially correct
21 Correct 10 ms 344 KB Output is correct
22 Correct 9 ms 344 KB Output is correct
23 Correct 24 ms 344 KB Output is correct
24 Correct 26 ms 344 KB Output is correct
25 Correct 20 ms 344 KB Output is correct
26 Partially correct 179 ms 700 KB Output is partially correct
27 Partially correct 139 ms 2752 KB Output is partially correct
28 Partially correct 152 ms 696 KB Output is partially correct
29 Partially correct 256 ms 440 KB Output is partially correct
30 Partially correct 282 ms 600 KB Output is partially correct
31 Partially correct 239 ms 2640 KB Output is partially correct
32 Correct 9 ms 344 KB Output is correct
33 Correct 10 ms 344 KB Output is correct
34 Correct 25 ms 344 KB Output is correct
35 Correct 23 ms 344 KB Output is correct
36 Correct 29 ms 600 KB Output is correct
37 Partially correct 141 ms 692 KB Output is partially correct
38 Partially correct 138 ms 696 KB Output is partially correct
39 Partially correct 154 ms 696 KB Output is partially correct
40 Partially correct 253 ms 592 KB Output is partially correct
41 Partially correct 282 ms 492 KB Output is partially correct
42 Partially correct 243 ms 2544 KB Output is partially correct
43 Partially correct 735 ms 452 KB Output is partially correct
44 Partially correct 750 ms 460 KB Output is partially correct
45 Partially correct 750 ms 2568 KB Output is partially correct
46 Partially correct 765 ms 2508 KB Output is partially correct
47 Partially correct 745 ms 2500 KB Output is partially correct
48 Partially correct 732 ms 456 KB Output is partially correct
49 Partially correct 774 ms 452 KB Output is partially correct
50 Partially correct 700 ms 700 KB Output is partially correct
51 Partially correct 790 ms 712 KB Output is partially correct
52 Partially correct 724 ms 460 KB Output is partially correct
53 Partially correct 710 ms 452 KB Output is partially correct
54 Partially correct 707 ms 452 KB Output is partially correct
55 Partially correct 683 ms 856 KB Output is partially correct
56 Partially correct 793 ms 600 KB Output is partially correct
57 Partially correct 730 ms 452 KB Output is partially correct
58 Partially correct 739 ms 456 KB Output is partially correct
59 Partially correct 737 ms 2384 KB Output is partially correct
60 Partially correct 720 ms 448 KB Output is partially correct
61 Partially correct 713 ms 452 KB Output is partially correct
62 Partially correct 722 ms 600 KB Output is partially correct
63 Partially correct 710 ms 2548 KB Output is partially correct
64 Partially correct 732 ms 2556 KB Output is partially correct
65 Partially correct 688 ms 532 KB Output is partially correct
66 Partially correct 691 ms 540 KB Output is partially correct
67 Partially correct 770 ms 548 KB Output is partially correct
68 Partially correct 754 ms 856 KB Output is partially correct
69 Partially correct 767 ms 620 KB Output is partially correct
70 Partially correct 779 ms 548 KB Output is partially correct
71 Partially correct 722 ms 512 KB Output is partially correct
72 Partially correct 717 ms 516 KB Output is partially correct
73 Partially correct 689 ms 532 KB Output is partially correct
74 Partially correct 758 ms 856 KB Output is partially correct
75 Partially correct 721 ms 556 KB Output is partially correct
76 Partially correct 727 ms 600 KB Output is partially correct
77 Partially correct 742 ms 600 KB Output is partially correct
78 Partially correct 766 ms 780 KB Output is partially correct
79 Partially correct 709 ms 792 KB Output is partially correct
80 Partially correct 706 ms 2564 KB Output is partially correct
81 Partially correct 763 ms 2580 KB Output is partially correct
82 Partially correct 762 ms 600 KB Output is partially correct