답안 #379550

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
379550 2021-03-18T14:21:26 Z SavicS Islands (IOI08_islands) C++14
51 / 100
612 ms 131076 KB
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>

#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define ff(i,a,b) for(int i=a;i<=b;i++)
#define fb(i,b,a) for(int i=b;i>=a;i--)

using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,ll> pii;
const int maxn = 1000005;
const int inf = 1e9 + 5;

template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

// os.order_of_key(k) the number of elements in the os less than k
// *os.find_by_order(k)  print the k-th smallest number in os(0-based)

int n;

vector<pii> g[maxn];

map<pii,ll> kol;

vector<pii> krug;
int par[maxn];
int visited[maxn];
void dfs1(int v, int p){
	par[v] = p;
	visited[v] = 1;
	for(auto c : g[v]){
		int u = c.fi;
		ll w = c.se;
		if(w < 0)continue;
//		if(u == p)continue;
		if(!visited[u])dfs1(u, v);
		else{
			if(visited[u] == 1){
				int a = v;
				ll nxt = 0;
				while(a != u){
					nxt = kol[{a, par[a]}];
					krug.pb({a, nxt});
					a = par[a];
				}
				krug.pb({u, kol[{u, v}]});
			}
		}
	}
	visited[v] = 2;
}

ll dia = 0;
bool was[maxn];
ll dfs2(int v, ll duz){
	was[v] = 1;
	ll best = duz;
	for(auto c : g[v]){
		int u = c.fi;
		ll w = abs(c.se);
		if(!was[u]){
			ll dist = dfs2(u, duz + w);
			dia = max(dia, dist + best - 2 * duz);
			best = max(best, dist);
		}
	}		
	return best;
}

ll rez = 0;
ll pref[maxn];
ll sta[maxn];
void Solve(int x){
	dia = 0;
	krug.clear();
	dfs1(x, -1);
	for(auto c : krug)was[c.fi] = 1;
	ff(i,0,sz(krug) - 1)sta[i] = dfs2(krug[i].fi, 0);
	int len = sz(krug);
	pref[0] = 0;
	ff(i,0,len - 1)pref[i + 1] = pref[i] + krug[i].se;
	ll zbir = pref[len];
	ll mn = -sta[0];
	ll mx = sta[0];
	ll najv = 0;
	ff(i,1,len - 1){
		najv = max(najv, pref[i] + sta[i] - mn);
		najv = max(najv, zbir + sta[i] - pref[i] + mx);
		mn = min(mn, pref[i] - sta[i]);
		mx = max(mx, pref[i] + sta[i]);
	}
	rez += max(najv, dia);
}

int main()
{
    ios::sync_with_stdio(false);
    cout.tie(nullptr);
    cin.tie(nullptr);
	cin >> n;
	ff(i,1,n){
		int a;
		ll b;
		cin >> a >> b;
		g[i].pb({a, b});
		g[a].pb({i, -b});
		kol[{i, a}] = kol[{a, i}] = b;
	}
	ff(i,1,n){
		if(!visited[i]){
			Solve(i);
		}
	}
	cout << rez << endl;
    return 0;
}
/**

7
3 8
7 2
4 2
1 4
1 9
3 4
2 3 


// probati bojenje sahovski ili slicno

**/

# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 23916 KB Output is correct
2 Correct 21 ms 23916 KB Output is correct
3 Correct 17 ms 24044 KB Output is correct
4 Correct 17 ms 23916 KB Output is correct
5 Correct 16 ms 23916 KB Output is correct
6 Incorrect 17 ms 23916 KB Output isn't correct
7 Incorrect 17 ms 24064 KB Output isn't correct
8 Correct 16 ms 23916 KB Output is correct
9 Incorrect 17 ms 23916 KB Output isn't correct
10 Correct 17 ms 23916 KB Output is correct
11 Incorrect 19 ms 23916 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 24172 KB Output is correct
2 Incorrect 20 ms 24044 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 19 ms 24300 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 44 ms 27372 KB Output is correct
2 Correct 92 ms 33904 KB Output is correct
3 Correct 46 ms 28140 KB Output is correct
4 Correct 28 ms 25964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 93 ms 37740 KB Output is correct
2 Correct 107 ms 47076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 271 ms 67176 KB Output is correct
2 Correct 270 ms 82084 KB Output is correct
3 Correct 324 ms 102744 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 376 ms 106092 KB Output is correct
2 Runtime error 368 ms 131072 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 612 ms 131076 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 570 ms 131076 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -