답안 #118115

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
118115 2019-06-18T08:13:56 Z baluteshih Islands (IOI08_islands) C++14
70 / 100
693 ms 131072 KB
#pragma GCC optimize("-Wl,--stack,536870912")
#include <bits/stdc++.h>
#define jizz ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define pb push_back
#define ET cout << "\n"
#define MEM(i,j) memset(i,j,sizeof i)
#define F first
#define S second
#define MP make_pair
#define ALL(v) v.begin(),v.end()
#define DB(a,s,e) {for(int i=s;i<e;++i) cout << a[i] << " ";ET;}
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
 
struct mode
{
	ll F,S,num;
};
 
vector<mode> G[1000005];
vector<pll> cir;
vector<ll> len;
bitset<1000005> yc;
ll M,dft,dfn[1000005];
const ll INF=1e18;
 
bool dfs(ll u,ll f)
{
	ll flag=0;
	dfn[u]=++dft;
	for(auto i:G[u])
		if(i.num!=f)
			if(!dfn[i.F])
				if(dfs(i.F,i.num))
					flag=1,len.pb(i.S);
				else;
			else if(dfn[i.F]<=dfn[u])
				flag=1,yc[i.F]=1,cir.pb(MP(i.F,0)),len.pb(i.S);
	if(flag)
		if(yc[u]) flag=0;
		else cir.pb(MP(u,0)),yc[u]=1;
	return flag;
}
 
ll dfs2(ll u)
{
	ll mx=0,sx=0;
	yc[u]=1;
	for(auto i:G[u])
		if(!yc[i.F])
		{
			ll d=dfs2(i.F)+i.S;
			if(d>mx) sx=mx,mx=d;
			else if(d>sx) sx=d;
		}
	M=max(M,sx+mx);
	return mx;
}
 
int main()
{jizz
	ll n,a,w,ans=0,sum;
	cin >> n;
	for(int i=1;i<=n;++i)
	{
		cin >> a >> w;
		G[i].pb(mode{a,w,i});
		if(a!=i) G[a].pb(mode{i,w,i});
	}
	for(int i=1;i<=n;++i)
		if(!dfn[i])
		{
			cir.clear(),len.clear(),M=0,dfs(i,-1);
			for(ll j=0;j<cir.size();++j)
				cir[j].S=dfs2(cir[j].F);
			if(cir.size()>1)
			{
				ll n=cir.size();
				deque<pll> dq;
				sum=0,dq.pb(MP(0,cir[0].S));
				for(ll j=0;j<n;++j)	
					cir.pb(cir[j]),len.pb(len[j]);
				for(ll j=1;j<cir.size();++j)
				{
					while(dq.size()&&j-dq[0].F>=n) dq.pop_front();
					sum+=len[j-1],M=max(M,sum+cir[j].S+dq[0].S);
					while(dq.size()&&cir[j].S-sum>=dq.back().S)
						dq.pop_back();
					dq.pb(MP(j,cir[j].S-sum));
				}
			}
			ans+=M;
		}
	cout << ans << "\n";
}

Compilation message

islands.cpp:1:45: warning: bad option '-Wl' to pragma 'optimize' [-Wpragmas]
 #pragma GCC optimize("-Wl,--stack,536870912")
                                             ^
islands.cpp:1:45: warning: bad option '--stack' to pragma 'optimize' [-Wpragmas]
islands.cpp:29:19: warning: bad option '-Wl' to attribute 'optimize' [-Wattributes]
 bool dfs(ll u,ll f)
                   ^
islands.cpp:29:19: warning: bad option '--stack' to attribute 'optimize' [-Wattributes]
islands.cpp: In function 'bool dfs(ll, ll)':
islands.cpp:34:5: warning: suggest explicit braces to avoid ambiguous 'else' [-Wdangling-else]
   if(i.num!=f)
     ^
islands.cpp:41:4: warning: suggest explicit braces to avoid ambiguous 'else' [-Wdangling-else]
  if(flag)
    ^
islands.cpp: At global scope:
islands.cpp:47:13: warning: bad option '-Wl' to attribute 'optimize' [-Wattributes]
 ll dfs2(ll u)
             ^
islands.cpp:47:13: warning: bad option '--stack' to attribute 'optimize' [-Wattributes]
islands.cpp:62:10: warning: bad option '-Wl' to attribute 'optimize' [-Wattributes]
 int main()
          ^
islands.cpp:62:10: warning: bad option '--stack' to attribute 'optimize' [-Wattributes]
islands.cpp: In function 'int main()':
islands.cpp:76:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for(ll j=0;j<cir.size();++j)
               ~^~~~~~~~~~~
islands.cpp:85:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(ll j=1;j<cir.size();++j)
                ~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 23808 KB Output is correct
2 Correct 25 ms 23936 KB Output is correct
3 Correct 24 ms 23936 KB Output is correct
4 Correct 23 ms 23808 KB Output is correct
5 Correct 24 ms 23808 KB Output is correct
6 Correct 24 ms 23800 KB Output is correct
7 Correct 23 ms 23808 KB Output is correct
8 Correct 23 ms 23928 KB Output is correct
9 Correct 24 ms 23800 KB Output is correct
10 Correct 23 ms 23808 KB Output is correct
11 Correct 24 ms 23936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 23936 KB Output is correct
2 Correct 24 ms 24056 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 24052 KB Output is correct
2 Correct 27 ms 24576 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 25468 KB Output is correct
2 Correct 51 ms 29816 KB Output is correct
3 Correct 38 ms 25848 KB Output is correct
4 Correct 29 ms 24824 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 58 ms 31576 KB Output is correct
2 Correct 67 ms 35440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 122 ms 42432 KB Output is correct
2 Correct 140 ms 55660 KB Output is correct
3 Correct 173 ms 74628 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 202 ms 56220 KB Output is correct
2 Correct 284 ms 100436 KB Output is correct
3 Correct 337 ms 115928 KB Output is correct
4 Runtime error 344 ms 131072 KB Execution killed with signal 9 (could be triggered by violating memory limits)
# 결과 실행 시간 메모리 Grader output
1 Correct 380 ms 86608 KB Output is correct
2 Runtime error 693 ms 131072 KB Execution killed with signal 9 (could be triggered by violating memory limits)
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 390 ms 131072 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -