Submission #347880

# Submission time Handle Problem Language Result Execution time Memory
347880 2021-01-13T17:25:34 Z AmineWeslati Fun Tour (APIO20_fun) C++14
31 / 100
190 ms 20068 KB
//Never stop trying
/*#pragma GCC target ("avx2")
#pragma GCC optimize ("Ofast")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")*/
#include "bits/stdc++.h"
#ifndef LOCAL
#include "fun.h"
#endif
using namespace std;
#define boost ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)

typedef long long ll;
typedef string str;
typedef double db;
typedef long double ld;
typedef pair<int, int> pi;
#define fi first
#define se second
typedef vector<int> vi;
typedef vector<pi> vpi;
typedef vector<str> vs;
typedef vector<ld> vd;
#define pb push_back
#define eb emplace_back
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define endl "\n"

#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)

const int MOD = 1e9 + 7; //998244353
const ll INF = 2e9;
const int MX = 2e5 + 10;
const int nx[4] = {0, 0, 1, -1}, ny[4] = {1, -1, 0, 0}; //right left down up

template<class T> using V = vector<T>;
template<class T> bool ckmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }

ll cdiv(ll a, ll b) { return a / b + ((a ^ b) > 0 && a % b); } // divide a by b rounded up
//constexpr int log2(int x) { return 31 - __builtin_clz(x); } // floor(log2(x))

mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
//mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());

ll random(ll a, ll b){
    return a + rng() % (b - a + 1);
}

#ifndef LOCAL  
#define cerr if(false) cerr
#endif
#define dbg(x) cerr << #x << " : " << x << endl; 
#define dbgs(x,y) cerr << #x << " : " << x << " / " << #y << " : " << y << endl;
#define dbgv(v) cerr << #v << " : " << "[ "; for(auto it : v) cerr << it << ' '; cerr << ']' << endl;
#define here() cerr << "here" << endl;

void IO() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}

#define subtree attractionsBehind
#define dist hoursRequired

int N,C; 
vi dC(MX),ans,comp(MX);
set<pi> s[3];

#ifdef LOCAL
vi adj[MX],dis(MX),subt(MX);
void dfs(int u, int p){
	subt[u]=1;
	for(auto v: adj[u]) if(v!=p){
		dis[v]=dis[u]+1;
		dfs(v,u);
		subt[u]+=subt[v];
	}
}
int hoursRequired(int u, int v){
	dis[u]=0;
	dfs(u,u);
	return dis[v];
}
int attractionsBehind(int u, int v){
	dfs(u,u);
	return subt[v];
}
#endif

int find_centroid(){
	int ans,mn=INF;
	FOR(i,0,N){
		int x=subtree(0,i);
		if(x>=(N+1)/2 && ckmin(mn,x)) ans=i;
	}
	return ans;
}

void decompose(){
	FOR(i,0,N) dC[i]=dist(C,i);

	vi v;
	FOR(i,0,N) if(dC[i]==1) v.pb(i);
	assert(sz(v)>=2);
	
	FOR(i,0,N) if(i!=C){
		int d1=dist(v[0],i),d2=dist(v[1],i);
		if(dC[i]==d1+1) s[0].insert({dC[i],i}),comp[i]=0;
		else if(dC[i]==d2+1) s[1].insert({dC[i],i}),comp[i]=1;
		else s[2].insert({dC[i],i}),comp[i]=2;
	}
}

int farthest(){
	int mx=0,ans;
	FOR(i,0,N) if(ckmax(mx,dC[i])) ans=i;
	return ans;
}
bool check(){
	vi v={sz(s[0]),sz(s[1]),sz(s[2])};
	sort(all(v));
	return v[0] + v[1] > v[2];
}
void del(int u){
	FOR(i,0,3) if(s[i].count({dC[u],u})) s[i].erase({dC[u],u});
	ans.pb(u);
}

int find_nxt(int u){
	int nxt=-1,mx=-1;
	FOR(i,0,3) if(i!=comp[u] && !s[i].empty()){
		auto it=s[i].end(); it--;
		if(ckmax(mx,(*it).fi)) nxt=(*it).se;
	} 
	return nxt;
}


void combine(){
	vpi v={{sz(s[0]),0},{sz(s[1]),1},{sz(s[2]),2}};
	sort(all(v));

	int i=v[0].se,j=v[1].se,k=v[2].se;
	if(i>j) swap(i,j);

	for(auto x: s[j]) s[i].insert(x);
	s[j].clear();

	if(k==2){
		assert(i==0 && j==1 && s[1].empty());
		for(auto x: s[2]) s[1].insert(x);
		s[2].clear();
	}
	
	if(sz(s[0])<sz(s[1])) swap(s[0],s[1]);

	for(auto x: s[0]) comp[x.se]=0;
	for(auto x: s[1]) comp[x.se]=1;
}

vi createFunTour(int N, int Q) {
	::N=N;
	if(N==2) return {0,1};

	C=find_centroid();
	decompose();

	int cur=farthest(),nxt=find_nxt(cur),first=1;
	while(check()){
		if(first) del(cur);
		del(nxt);
		cur=nxt;
		nxt=find_nxt(cur);
		first=0;
	}

	combine();

	if(first){
		auto it=s[0].end(); it--; cur=(*it).se;
		it=s[1].end(); it--; nxt=(*it).se;
	}

	while(sz(s[0])||sz(s[1])){
		if(first) del(cur);
		del(nxt);
		cur=nxt;
		nxt=find_nxt(cur);
		first=0;
	}
	ans.pb(C);
	assert(sz(ans)==N);
	return ans;
}

#ifdef LOCAL
int main() {
    boost; IO();

    int N,Q; cin>>N>>Q;
    FOR(i,0,N-1){
    	int u,v; cin>>u>>v;
    	adj[u].pb(v);
    	adj[v].pb(u);
    }
    vi ans=createFunTour(N,0);
    cout << sz(ans) << endl;
    for(auto x: ans) cout << x << ' '; cout << endl;
    FOR(i,1,sz(ans)-1) 
		assert(dist(ans[i],ans[i+1])<=dist(ans[i-1],ans[i]));
   	

    return 0;
}
#endif

/* Careful!!!
    .Array bounds
    .Infinite loops
    .Uninitialized variables / empty containers
    .Multisets are shit

   Some insights:
    .Binary search
    .Graph representation
    .Write brute force code
    .Change your approach
*/

Compilation message

fun.cpp: In function 'int find_centroid()':
fun.cpp:102:9: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
  102 |  return ans;
      |         ^~~
fun.cpp: In function 'int farthest()':
fun.cpp:123:9: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
  123 |  return ans;
      |         ^~~
fun.cpp: In function 'vi createFunTour(int, int)':
fun.cpp:174:33: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
  174 |  int cur=farthest(),nxt=find_nxt(cur),first=1;
      |                         ~~~~~~~~^~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1900 KB Output is correct
2 Correct 2 ms 1900 KB Output is correct
3 Correct 2 ms 1900 KB Output is correct
4 Correct 2 ms 1900 KB Output is correct
5 Correct 2 ms 1900 KB Output is correct
6 Correct 2 ms 1900 KB Output is correct
7 Correct 2 ms 1900 KB Output is correct
8 Correct 1 ms 1900 KB Output is correct
9 Correct 1 ms 1900 KB Output is correct
10 Correct 1 ms 1900 KB Output is correct
11 Correct 2 ms 1900 KB Output is correct
12 Correct 2 ms 1900 KB Output is correct
13 Correct 2 ms 1900 KB Output is correct
14 Correct 2 ms 1900 KB Output is correct
15 Correct 2 ms 1900 KB Output is correct
16 Correct 2 ms 1900 KB Output is correct
17 Correct 2 ms 1900 KB Output is correct
18 Correct 2 ms 1900 KB Output is correct
19 Correct 2 ms 1900 KB Output is correct
20 Correct 2 ms 1900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1900 KB Output is correct
2 Correct 2 ms 1900 KB Output is correct
3 Correct 2 ms 1900 KB Output is correct
4 Correct 2 ms 1900 KB Output is correct
5 Correct 2 ms 1900 KB Output is correct
6 Correct 2 ms 1900 KB Output is correct
7 Correct 2 ms 1900 KB Output is correct
8 Correct 1 ms 1900 KB Output is correct
9 Correct 1 ms 1900 KB Output is correct
10 Correct 1 ms 1900 KB Output is correct
11 Correct 2 ms 1900 KB Output is correct
12 Correct 2 ms 1900 KB Output is correct
13 Correct 2 ms 1900 KB Output is correct
14 Correct 2 ms 1900 KB Output is correct
15 Correct 2 ms 1900 KB Output is correct
16 Correct 2 ms 1900 KB Output is correct
17 Correct 2 ms 1900 KB Output is correct
18 Correct 2 ms 1900 KB Output is correct
19 Correct 2 ms 1900 KB Output is correct
20 Correct 2 ms 1900 KB Output is correct
21 Correct 2 ms 1900 KB Output is correct
22 Correct 2 ms 1900 KB Output is correct
23 Correct 2 ms 1900 KB Output is correct
24 Correct 2 ms 1900 KB Output is correct
25 Correct 2 ms 1900 KB Output is correct
26 Correct 2 ms 1900 KB Output is correct
27 Correct 2 ms 1900 KB Output is correct
28 Correct 2 ms 1920 KB Output is correct
29 Incorrect 2 ms 1900 KB Tour is not fun
30 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1900 KB Output is correct
2 Correct 2 ms 1900 KB Output is correct
3 Correct 2 ms 1900 KB Output is correct
4 Correct 2 ms 1900 KB Output is correct
5 Correct 2 ms 1900 KB Output is correct
6 Correct 2 ms 1900 KB Output is correct
7 Correct 1 ms 1900 KB Output is correct
8 Correct 1 ms 1900 KB Output is correct
9 Correct 2 ms 1900 KB Output is correct
10 Correct 2 ms 1900 KB Output is correct
11 Correct 2 ms 1900 KB Output is correct
12 Correct 2 ms 1900 KB Output is correct
13 Correct 2 ms 1900 KB Output is correct
14 Correct 2 ms 1900 KB Output is correct
15 Correct 2 ms 1900 KB Output is correct
16 Correct 2 ms 1900 KB Output is correct
17 Correct 2 ms 1900 KB Output is correct
18 Correct 190 ms 20068 KB Output is correct
19 Correct 3 ms 2028 KB Output is correct
20 Correct 4 ms 2156 KB Output is correct
21 Correct 6 ms 2412 KB Output is correct
22 Correct 10 ms 3052 KB Output is correct
23 Correct 20 ms 4332 KB Output is correct
24 Correct 26 ms 5100 KB Output is correct
25 Correct 108 ms 12676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1900 KB Output is correct
2 Correct 2 ms 1900 KB Output is correct
3 Correct 1 ms 1900 KB Output is correct
4 Correct 2 ms 1900 KB Output is correct
5 Correct 2 ms 1900 KB Output is correct
6 Correct 2 ms 1900 KB Output is correct
7 Correct 2 ms 1920 KB Output is correct
8 Incorrect 2 ms 1900 KB Tour is not fun
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1900 KB Output is correct
2 Correct 2 ms 1900 KB Output is correct
3 Correct 2 ms 1900 KB Output is correct
4 Correct 2 ms 1900 KB Output is correct
5 Correct 2 ms 1900 KB Output is correct
6 Correct 2 ms 1900 KB Output is correct
7 Correct 2 ms 1900 KB Output is correct
8 Correct 1 ms 1900 KB Output is correct
9 Correct 1 ms 1900 KB Output is correct
10 Correct 1 ms 1900 KB Output is correct
11 Correct 2 ms 1900 KB Output is correct
12 Correct 2 ms 1900 KB Output is correct
13 Correct 2 ms 1900 KB Output is correct
14 Correct 2 ms 1900 KB Output is correct
15 Correct 2 ms 1900 KB Output is correct
16 Correct 2 ms 1900 KB Output is correct
17 Correct 2 ms 1900 KB Output is correct
18 Correct 2 ms 1900 KB Output is correct
19 Correct 2 ms 1900 KB Output is correct
20 Correct 2 ms 1900 KB Output is correct
21 Correct 2 ms 1900 KB Output is correct
22 Correct 2 ms 1900 KB Output is correct
23 Correct 2 ms 1900 KB Output is correct
24 Correct 2 ms 1900 KB Output is correct
25 Correct 2 ms 1900 KB Output is correct
26 Correct 2 ms 1900 KB Output is correct
27 Correct 2 ms 1900 KB Output is correct
28 Correct 2 ms 1920 KB Output is correct
29 Incorrect 2 ms 1900 KB Tour is not fun
30 Halted 0 ms 0 KB -