Submission #393208

# Submission time Handle Problem Language Result Execution time Memory
393208 2021-04-23T00:38:52 Z AmineWeslati Islands (IOI08_islands) C++14
3 / 100
366 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/////////////////////////

//--------------------------------------------
int p[MX];
int findSet(int u){
	return p[u]==u ? 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) p[u]=u;
	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_val1,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:218:29: warning: value computed is not used [-Wunused-value]
  218 |   ckmax(ans,max_len[i]+s[i])+mx_val1;
      |   ~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
islands.cpp:221:31: warning: value computed is not used [-Wunused-value]
  221 |   ckmax(ans,S+max_len[i]-s[i])+mx_val2;
      |   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
islands.cpp: In function 'int get_farthest()':
islands.cpp:171:9: warning: 'u' may be used uninitialized in this function [-Wmaybe-uninitialized]
  171 |  return u;
      |         ^
islands.cpp: In function 'll compute_diam(int)':
islands.cpp:184:15: warning: 'u' may be used uninitialized in this function [-Wmaybe-uninitialized]
  184 |  ll ans=dist[v];
      |               ^
islands.cpp:171:9: warning: 'u' may be used uninitialized in this function [-Wmaybe-uninitialized]
  171 |  return u;
      |         ^
islands.cpp:169:11: note: 'u' was declared here
  169 |  ll mx=-1,u;
      |           ^
islands.cpp: In function 'void build_cycle()':
islands.cpp:118:6: warning: 'V' may be used uninitialized in this function [-Wmaybe-uninitialized]
  118 |  int cur=V;
      |      ^~~
islands.cpp:114:7: warning: 'U' may be used uninitialized in this function [-Wmaybe-uninitialized]
  114 |  par[U]={V,W};
      |       ^
# Verdict Execution time Memory Grader output
1 Incorrect 57 ms 98224 KB Output isn't correct
2 Incorrect 56 ms 98136 KB Output isn't correct
3 Incorrect 56 ms 98204 KB Output isn't correct
4 Incorrect 63 ms 98208 KB Output isn't correct
5 Incorrect 56 ms 98124 KB Output isn't correct
6 Incorrect 57 ms 98200 KB Output isn't correct
7 Incorrect 60 ms 98212 KB Output isn't correct
8 Incorrect 58 ms 98152 KB Output isn't correct
9 Incorrect 56 ms 98120 KB Output isn't correct
10 Correct 59 ms 98152 KB Output is correct
11 Incorrect 55 ms 98116 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 57 ms 98264 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 59 ms 98336 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 69 ms 100032 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 100 ms 105912 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 216 ms 123560 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 243 ms 131076 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 292 ms 131076 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 366 ms 131076 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -