답안 #1054753

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1054753 2024-08-12T11:56:53 Z lovrot Islands (IOI08_islands) C++17
15 / 100
191 ms 131072 KB
#include <cstdio> 
#include <vector>
#include <algorithm> 

#define debug(...) //fprintf(stderr, __VA_ARGS__)
#define X first
#define Y second
#define PB push_back

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int N = 1e6 + 10;

int n;
ll weg[N]; 
vector<pii> g[N];

int bio[N];
vector<pair<int, ll>> cyc;

int dfs(int u, int p) { 
	bio[u] = 1;
	for(pii e : g[u]) { 
		int v = e.X, w = e.Y;
		if(w != p) { 
			if(bio[v]) { 
				cyc.PB({e.X, weg[e.Y]});
				return v;
			} else {
				int res = dfs(v, w);
				if(res) { 
					cyc.PB({e.X, weg[e.Y]});
					return res != u ? res : 0;
				}
			}
		}
	}
	bio[u] = 0;
	return 0;
}

void mark(int u) { 
	bio[u] = 2;
	for(pii e : g[u]) { 
		if(bio[e.X] != 2) { 
			mark(e.X);
		}
	}
}

ll dep[N];

ll depth(int u, int p) { 
	for(pii e : g[u]) { 
		int v = e.X, w = e.Y;
		if(w != p && !bio[v]) { 
			dep[u] = max(dep[u], (ll) weg[w] + depth(v, w));
		}
	}
	return dep[u];
}

int main() { 
	scanf("%d", &n);
	for(int i = 1; i <= n; ++i) { 
		int u;
		scanf("%d%d", &u, weg + i);
		g[i].PB({u, i});
		g[u].PB({i, i});
	}

	ll ans = 0;
	for(int i = 1; i <= n; ++i) { 
		if(bio[i]) { continue; }

		cyc.clear();
		dfs(i, 0);

		for(pii e : cyc) { 
			depth(e.X, 0);
			debug("%d(%d, %lld)\n", e.X, e.Y, dep[e.X]);
		}

		ll res = dep[cyc.front().X], xam = dep[cyc.front().X], prf = cyc.front().Y;
		for(int i = 1; i < cyc.size(); ++i) { 
			res = max(dep[cyc[i].X] + xam + prf, res);
			xam = max(dep[cyc[i].X] - prf, xam);
			prf += (ll) cyc[i].Y;
		}
	
		xam = dep[cyc.front().X];
		prf = 0;

		for(int i = (int) cyc.size() - 1; i > 0; --i) { 
			prf += cyc[i].Y;
			res = max(dep[cyc[i].X] + xam + prf, res);
			xam = max(dep[cyc[i].X] - prf, xam);
		}

		ans += res;
		mark(i);
		debug("+ %lld\n", res);
	}

	printf("%lld\n", ans);
	return 0;
}

Compilation message

islands.cpp: In function 'int main()':
islands.cpp:70:13: warning: format '%d' expects argument of type 'int*', but argument 3 has type 'll*' {aka 'long long int*'} [-Wformat=]
   70 |   scanf("%d%d", &u, weg + i);
      |            ~^       ~~~~~~~
      |             |           |
      |             int*        ll* {aka long long int*}
      |            %lld
islands.cpp:88:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   88 |   for(int i = 1; i < cyc.size(); ++i) {
      |                  ~~^~~~~~~~~~~~
islands.cpp:67:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   67 |  scanf("%d", &n);
      |  ~~~~~^~~~~~~~~~
islands.cpp:70:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   70 |   scanf("%d%d", &u, weg + i);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 26972 KB Output is correct
2 Incorrect 3 ms 26972 KB Output isn't correct
3 Incorrect 3 ms 27080 KB Output isn't correct
4 Correct 3 ms 27080 KB Output is correct
5 Correct 3 ms 26968 KB Output is correct
6 Correct 3 ms 26972 KB Output is correct
7 Incorrect 3 ms 26972 KB Output isn't correct
8 Incorrect 3 ms 26972 KB Output isn't correct
9 Incorrect 3 ms 27064 KB Output isn't correct
10 Incorrect 3 ms 26972 KB Output isn't correct
11 Correct 3 ms 27072 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 5 ms 27228 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 26972 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 8 ms 27996 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 31956 KB Output is correct
2 Incorrect 20 ms 35544 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 43 ms 40656 KB Output is correct
2 Correct 48 ms 46536 KB Output is correct
3 Incorrect 61 ms 60316 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 75 ms 50040 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 146 ms 75580 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 191 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -