Submission #1065264

# Submission time Handle Problem Language Result Execution time Memory
1065264 2024-08-19T05:30:58 Z pcc Longest Trip (IOI23_longesttrip) C++17
25 / 100
882 ms 856 KB
#include "longesttrip.h"
#include <bits/stdc++.h>
using namespace std;

#define vi vector<int>

const int mxn = 300;

int ask(vector<int> a,vector<int> b){
	return are_connected(a,b);
}

int dp[mxn][mxn];
bitset<mxn> vis;
int N;

int ask(int a,int b){
	if(dp[a][b] != -1)return dp[a][b];
	return ask(vector<int>({a}),vector<int>({b}));
}

void dfs(int now){
	vis[now] = true;
	for(int i = 0;i<N;i++){
		if(dp[i]&&!vis[i])dfs(i);
	}
	return;
}

vector<int> get_max_cc(){
	cerr<<"GETTING MAX CC: "<<endl;
	vector<int> heads;
	vis.reset();
	for(int i = 0;i<N;i++){
		if(!vis[i]){
			heads.push_back(i);
			dfs(i);
		}
	}
	int ans = 0,tar = -1;
	for(auto &i:heads){
		vis.reset();
		dfs(i);
		if(vis.count()>ans){
			ans = vis.count();
			tar = i;
		}
	}
	assert(tar != -1);
	dfs(tar);
	vector<int> re;
	for(int i = 0;i<N;i++)if(vis[i])re.push_back(i);
	return re;
}

std::vector<int> longest_trip(int NN, int D){
	N = NN;
	memset(dp,-1,sizeof(dp));
	vis.reset();
	for(int i = 0;i<N;i++){
		dp[i][i] = 1;
		for(int j = i+1;j<N;j++)dp[i][j] = dp[j][i] = ask(i,j);
	}
	dfs(1);
	if(vis.count() != N){
		return get_max_cc();
	}
	deque<int> dq[2];
	set<int> st;
	for(int i = 0;i<N;i++)st.insert(i);
	if(ask(0,1))dq[0] = {0,1};
	else if(ask(1,2))dq[0] = {1,2};
	else dq[0] = {0,2};
	for(auto &i:dq[0])st.erase(i);
	while(st.size()){
		if(dq[1].empty()){
			dq[1].push_back(*st.begin());
			st.erase(dq[1][0]);
			continue;
		}
		auto now = *st.begin();
		st.erase(now);
		if(ask(now,dq[0].back())){
			dq[0].push_back(now);
		}
		else if(ask(now,dq[1].back())){
			dq[1].push_back(now);
		}
		else{
			while(!dq[1].empty()){
				dq[0].push_back(dq[1].back());
				dq[1].pop_back();
			}
			dq[1] = {now};
		}
	}
	if(dq[0].size()<dq[1].size())dq[0].swap(dq[1]);
	/*
	cerr<<"DQ: "<<endl;
	for(auto &i:dq[0])cerr<<i<<' ';cerr<<endl;
	for(auto &i:dq[1])cerr<<i<<' ';cerr<<endl;
	cerr<<endl;
	*/
	vector<int> ans;
	for(auto &i:dq[0])ans.push_back(i);
	return ans;
}

Compilation message

longesttrip.cpp: In function 'std::vector<int> get_max_cc()':
longesttrip.cpp:44:17: warning: comparison of integer expressions of different signedness: 'std::size_t' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   44 |   if(vis.count()>ans){
      |      ~~~~~~~~~~~^~~~
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:65:17: warning: comparison of integer expressions of different signedness: 'std::size_t' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   65 |  if(vis.count() != N){
      |     ~~~~~~~~~~~~^~~~
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 600 KB Incorrect
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 600 KB Incorrect
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 600 KB Incorrect
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 9 ms 600 KB Output is correct
2 Correct 29 ms 600 KB Output is correct
3 Correct 153 ms 792 KB Output is correct
4 Correct 402 ms 600 KB Output is correct
5 Correct 800 ms 764 KB Output is correct
6 Correct 12 ms 600 KB Output is correct
7 Correct 24 ms 600 KB Output is correct
8 Correct 159 ms 600 KB Output is correct
9 Correct 317 ms 600 KB Output is correct
10 Correct 857 ms 772 KB Output is correct
11 Correct 814 ms 764 KB Output is correct
12 Correct 814 ms 764 KB Output is correct
13 Correct 854 ms 768 KB Output is correct
14 Correct 10 ms 600 KB Output is correct
15 Correct 17 ms 600 KB Output is correct
16 Correct 50 ms 600 KB Output is correct
17 Correct 90 ms 600 KB Output is correct
18 Correct 157 ms 792 KB Output is correct
19 Correct 326 ms 780 KB Output is correct
20 Correct 304 ms 600 KB Output is correct
21 Correct 856 ms 768 KB Output is correct
22 Correct 825 ms 768 KB Output is correct
23 Correct 853 ms 768 KB Output is correct
24 Correct 811 ms 772 KB Output is correct
25 Correct 13 ms 600 KB Output is correct
26 Correct 9 ms 600 KB Output is correct
27 Correct 19 ms 600 KB Output is correct
28 Correct 18 ms 600 KB Output is correct
29 Correct 25 ms 600 KB Output is correct
30 Correct 206 ms 596 KB Output is correct
31 Correct 179 ms 856 KB Output is correct
32 Correct 207 ms 856 KB Output is correct
33 Correct 322 ms 772 KB Output is correct
34 Correct 317 ms 848 KB Output is correct
35 Correct 347 ms 600 KB Output is correct
36 Correct 815 ms 852 KB Output is correct
37 Correct 880 ms 768 KB Output is correct
38 Correct 851 ms 768 KB Output is correct
39 Correct 854 ms 768 KB Output is correct
40 Correct 842 ms 768 KB Output is correct
41 Correct 882 ms 848 KB Output is correct
42 Correct 818 ms 764 KB Output is correct
43 Correct 871 ms 768 KB Output is correct
44 Correct 874 ms 768 KB Output is correct
45 Correct 13 ms 600 KB Output is correct
46 Correct 12 ms 596 KB Output is correct
47 Correct 22 ms 600 KB Output is correct
48 Correct 27 ms 600 KB Output is correct
49 Correct 23 ms 600 KB Output is correct
50 Correct 202 ms 600 KB Output is correct
51 Correct 205 ms 788 KB Output is correct
52 Correct 215 ms 600 KB Output is correct
53 Correct 312 ms 764 KB Output is correct
54 Correct 288 ms 600 KB Output is correct
55 Correct 311 ms 768 KB Output is correct
56 Correct 865 ms 768 KB Output is correct
57 Correct 806 ms 772 KB Output is correct
58 Correct 753 ms 764 KB Output is correct
59 Correct 824 ms 768 KB Output is correct
60 Correct 869 ms 848 KB Output is correct
61 Correct 877 ms 768 KB Output is correct
62 Correct 810 ms 768 KB Output is correct
63 Correct 691 ms 768 KB Output is correct
64 Correct 687 ms 772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 600 KB Incorrect
2 Halted 0 ms 0 KB -