Submission #393209

# Submission time Handle Problem Language Result Execution time Memory
393209 2021-04-23T00:44:08 Z AmineWeslati Islands (IOI08_islands) C++14
3 / 100
332 ms 131076 KB
//Never stop trying
#include "bits/stdc++.h"
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 long double ld;
typedef pair<int, int> pi;
#define fi first
#define se second
typedef vector<int> vi;
typedef vector<pi> vpi;
#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 = 1e18;
const int MX = 1e6 + 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
}
/////////////////////////ONLY CLEAN CODES ALLOWED/////////////////////////

//--------------------------------------------
vi p(MX,-1);
int findSet(int u){
	return p[u]==-1 ? u : p[u]=findSet(p[u]);
}

void joinSet(int u, int v){
	u=findSet(u); v=findSet(v);
	p[v]=u;
}
//-------------------------------------------------

int N; 
vpi adj[MX];

vi vec,vis(MX,0);
void divide(int u){
	if(vis[u]) return; 
	vis[u]=1;
	vec.pb(u);

	for(auto it: adj[u]){
		int v=it.fi; 
		divide(v);
	}
}

//Cycle building
vpi tree[MX];
vpi par(MX);

void find_cycle(int u, int pr){
	for(auto it: tree[u]){
		int v=it.fi,w=it.se;
		if(v!=pr){
			par[v]={u,w};
			find_cycle(v,u);
		}
	}
}

vpi cy;
void build_cycle(){
	int U,V,W; 
	for(int u: vec){
		for(auto it: adj[u]){
			int v=it.fi,w=it.se;
			if(u<v){
				if(findSet(u)!=findSet(v)){
					joinSet(u,v);
					tree[u].eb(v,w);
					tree[v].eb(u,w);
				}
				else U=u,V=v,W=w;
			}
		}
	}

	par[U]={V,W};
	find_cycle(U,U);

	cy.clear();
	int cur=V;
	while(1){
		cy.eb(cur,par[cur].se);
		if(cur==U) break;
		cur=par[cur].fi;
	}
}

//new graph building
vpi temp[MX];
vi v1(MX,-1),v2(MX,-1);
void build_graph(){
	int n=sz(cy);

	FOR(i,0,n){
		int u=cy[i].fi; 
		v1[u]=cy[(i-1+n)%n].fi,v2[u]=cy[(i+1)%n].fi;
	}

	for(int u: vec){
		for(auto it: adj[u]){
			int v=it.fi,w=it.se;
			if(v==v1[u] || v==v2[u]) continue;
			temp[u].eb(v,w);
		}
	}

	for(int u: vec){
		adj[u].assign(all(temp[u]));
	}

}

//computing
ll max_len[MX],diam[MX],S,s[MX];

V<ll> dist(MX,-1); 
vi viss;
void dfs(int u){
	viss.pb(u);
	for(auto it: adj[u]){
		int v=it.fi,w=it.se;
		if(dist[v]==-1){
			dist[v]=dist[u]+w;
			dfs(v);
		}
	}
}

int get_farthest(){
	ll mx=-1,u;
	for(int cur: viss) if(ckmax(mx,dist[cur])) u=cur;
	return u;
}

ll compute_diam(int u){
	dist[u]=0;
	dfs(u);
	u=get_farthest();
	for(int node: viss) dist[node]=-1;
	viss.clear();
	
	dist[u]=0;
	dfs(u);
	int v=get_farthest();
	ll ans=dist[v];
	for(int node: viss) dist[node]=-1;
	viss.clear();
	
	return ans;
}

ll compute_max_len(int u, int p){
	ll ans=0;
	for(auto it: adj[u]){
		int v=it.fi,w=it.se;
		if(v!=p) ckmax(ans,compute_max_len(v,u)+w);
	}
	return ans; 
}

ll compute(){
	S=0;
	int n=sz(cy);
	FOR(i,0,sz(cy)){
		S+=cy[i].se;

		if(!i) s[i]=0;
		else s[i]=s[i-1]+cy[i-1].se;

		diam[i]=compute_diam(cy[i].fi);
		max_len[i]=compute_max_len(cy[i].fi,cy[i].fi);
	}
	
	ll ans=0;
	FOR(i,0,n) ckmax(ans,diam[i]);

	ll mx_val1=0,mx_val2=0;
	FOR(i,0,n){
		ckmax(ans,max_len[i]+s[i])+mx_val1;
		ckmax(mx_val1,max_len[i]-s[i]);

		ckmax(ans,S+max_len[i]-s[i])+mx_val2;
		ckmax(mx_val2,max_len[i]+s[i]);
	}

	return ans;
}

int main() {
    boost; IO();

    cin>>N;
    FOR(i,1,N+1){
    	int j,w; cin>>j>>w;
    	adj[i].eb(j,w);
    	adj[j].eb(i,w);
    }

    ll ans=0;
    FOR(i,1,N+1) if(!vis[i]){
    	vec.clear();
    	divide(i); //new component

    	build_cycle(); 
    	build_graph();
    	ans+=compute();
    }
    cout << ans << endl;
    

    return 0;
}
//Change your approach 

Compilation message

islands.cpp: In function 'll compute()':
islands.cpp:216:29: warning: value computed is not used [-Wunused-value]
  216 |   ckmax(ans,max_len[i]+s[i])+mx_val1;
      |   ~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
islands.cpp:219:31: warning: value computed is not used [-Wunused-value]
  219 |   ckmax(ans,S+max_len[i]-s[i])+mx_val2;
      |   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
islands.cpp: In function 'int get_farthest()':
islands.cpp:169:9: warning: 'u' may be used uninitialized in this function [-Wmaybe-uninitialized]
  169 |  return u;
      |         ^
islands.cpp: In function 'll compute_diam(int)':
islands.cpp:182:15: warning: 'u' may be used uninitialized in this function [-Wmaybe-uninitialized]
  182 |  ll ans=dist[v];
      |               ^
islands.cpp:169:9: warning: 'u' may be used uninitialized in this function [-Wmaybe-uninitialized]
  169 |  return u;
      |         ^
islands.cpp:167:11: note: 'u' was declared here
  167 |  ll mx=-1,u;
      |           ^
islands.cpp: In function 'void build_cycle()':
islands.cpp:117:6: warning: 'V' may be used uninitialized in this function [-Wmaybe-uninitialized]
  117 |  int cur=V;
      |      ^~~
islands.cpp:113:7: warning: 'U' may be used uninitialized in this function [-Wmaybe-uninitialized]
  113 |  par[U]={V,W};
      |       ^
# Verdict Execution time Memory Grader output
1 Incorrect 57 ms 102080 KB Output isn't correct
2 Incorrect 58 ms 102140 KB Output isn't correct
3 Incorrect 58 ms 102160 KB Output isn't correct
4 Incorrect 64 ms 102100 KB Output isn't correct
5 Incorrect 57 ms 101980 KB Output isn't correct
6 Incorrect 57 ms 102168 KB Output isn't correct
7 Incorrect 56 ms 102084 KB Output isn't correct
8 Incorrect 58 ms 102032 KB Output isn't correct
9 Incorrect 57 ms 102060 KB Output isn't correct
10 Correct 63 ms 102036 KB Output is correct
11 Incorrect 57 ms 102048 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 59 ms 102156 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 59 ms 102212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 71 ms 103676 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 100 ms 109124 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 219 ms 126168 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 219 ms 131076 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 267 ms 131076 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 332 ms 131076 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -