Submission #42984308


Source Code Expand

// LUOGU_RID: 113367902
#include <bits/stdc++.h>
#define SZ(x) (int) x.size() - 1
#define all(x) x.begin(), x.end()
#define ms(x, y) memset(x, y, sizeof x)
#define F(i, x, y) for (int i = (x); i <= (y); i++)
#define DF(i, x, y) for (int i = (x); i >= (y); i--)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
template <typename T> void chkmax(T& x, T y) { x = max(x, y); }
template <typename T> void chkmin(T& x, T y) { x = min(x, y); }
template <typename T> void read(T &x) {
	x = 0; int f = 1; char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
	for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
	x *= f;
}
const int N = 2010;
int n, fa[N], far[N];
ll mx;
vector <int> v[N];
void dfs(int x, int fa) {
	far[x] = fa;
	for (int i: v[x])
		if (i != fa) dfs(i, x);
}
struct Node {
	int x, y, id;
	inline friend bool operator < (const Node &x, const Node &y) {
		if ((ll) x.x * y.y == (ll) y.x * x.y) return (x.id == y.id) ? x.y < y.y : x.id < y.id;
		return (ll) x.x * y.y < (ll) y.x * x.y;
	}
	inline friend bool operator == (const Node &x, const Node &y) {
		return x.x == y.x && x.y == y.y && x.id == y.id;
	}
} t[N];
int find(int x) {
	return fa[x] == x ? x : fa[x] = find(fa[x]);
}
template <typename _Tp> class heap {
    priority_queue<_Tp> _Val, _Del; bool _Op; size_t _Siz;
    void flush() {
        while(!_Del.empty() && !_Val.empty() && _Del.top() == _Val.top()) _Val.pop(), _Del.pop(); _Op = 0;
    }
    public:
    heap() : _Op(0), _Siz(0) {}
    size_t size() {return _Siz;}
    void push(const _Tp &x) {_Siz++; _Val.push(x);}
    _Tp top() {
        assert(_Siz); if(_Op) flush(); assert(!_Val.empty()); return _Val.top();
    }
    void pop() {
        assert(_Siz); _Siz--;
        if(_Op) flush(); _Op = (!_Del.empty());
        assert(!_Val.empty()); _Val.pop();
    }
    bool empty() {return _Siz == 0;}
    void erase(const _Tp &x) {
        assert(_Siz); _Siz--;
        if(_Op) flush(); assert(!_Val.empty());
        if(x == _Val.top()) {
            _Val.pop(); _Op = 1; return;
        }
        // cout << "# " << _Val.top().x << " " << _Val.top().y << " " << _Val.top().id << endl;
        assert(x < _Val.top()); _Del.push(x);
    }
    void clear() {
        _Op = _Siz = 0; priority_queue<_Tp> _Tx, _Ty;
        swap(_Val, _Tx), swap(_Del, _Ty);
    }
};
signed main() {
// 	freopen("dsu.in", "r", stdin);
// 	freopen("dsu.out", "w", stdout);
	read(n);
	F(i, 2, n) {
		int x, y; read(x), read(y); x++, y++;
		v[x].push_back(y);
		v[y].push_back(x);
	}
	F(i, 1, n) {
		dfs(i, 0);
		heap <Node> q;
		ll ans = 3 * (n - 1);
		F(j, 1, n) {
			fa[j] = j;
			t[j] = {(int) v[j].size() - 2, 1, j};
			// cout << "! " << t[j].x << endl;
			if (j != i) q.push(t[j]);//, cout << "+ " << j << endl;//, cout << "! " << j << " " << t[j].x << " " << t[j].y << " " << t[j].id << endl;
			ans += (ll) (n - 1) * ((int) v[j].size() - 2);
		}
		// cout << "> " << i << " " << n << " " << q.size() << " " << ans << endl;
		int s = 0;
		while (q.size()) {
			// if (++s > 450) {
			// 	while (q.size()) {
			// 		cout << q.top().x << " " << q.top().y << " " << q.top().id << endl;
			// 		q.pop();
			// 	}
			// 	return 0;
			// }
			int x = q.top().id;
			// cout << x << " " << t[x].x << " " << t[x].y << " " << t[x].id << endl;
			// cout << q.top().x << " " << q.top().y << " " << q.top().id << endl;
			// assert(q.top() == t[x]);
			// assert(t[x].id == x);
			// cout << "- " << x << endl;
			// cout << q.size() << " " << q.top().x << " " << q.top().y << " " << q.top().id << endl;
			q.pop();
			// cout << q.size() << " " << q.top().x << " " << q.top().y << " " << q.top().id << endl;
			int tx = find(far[x]);
			// cout << x << " " << tx << endl;
			if (tx != i) q.erase(t[tx]);//, cout << "-- " << tx << endl;
			ans -= (ll) t[tx].y * t[x].x;
			// cout << q.size() << " " << x << " " << t[tx].y << " " << t[x].x << endl;
			fa[x] = tx;
			t[tx].x += t[x].x;
			t[tx].y += t[x].y;
			if (tx != i) q.push(t[tx]);//, cout << "++ " << tx << endl;
		} chkmax(mx, ans);
		// cout << "! " << ans << endl;
	} cout << mx;
	return 0;
}

Submission Info

Submission Time
Task G - Git Gud
User ast123
Language C++ (GCC 9.2.1)
Score 1300
Code Size 4110 Byte
Status AC
Exec Time 1241 ms
Memory 3916 KiB

Compile Error

./Main.cpp: In member function ‘void heap<_Tp>::flush()’:
./Main.cpp:44:9: warning: this ‘while’ clause does not guard... [-Wmisleading-indentation]
   44 |         while(!_Del.empty() && !_Val.empty() && _Del.top() == _Val.top()) _Val.pop(), _Del.pop(); _Op = 0;
      |         ^~~~~
./Main.cpp:44:99: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the ‘while’
   44 |         while(!_Del.empty() && !_Val.empty() && _Del.top() == _Val.top()) _Val.pop(), _Del.pop(); _Op = 0;
      |                                                                                                   ^~~
./Main.cpp: In member function ‘void heap<_Tp>::pop()’:
./Main.cpp:55:9: warning: this ‘if’ clause does not guard... [-Wmisleading-indentation]
   55 |         if(_Op) flush(); _Op = (!_Del.empty());
      |         ^~
./Main.cpp:55:26: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the ‘if’
   55 |         if(_Op) flush(); _Op = (!_Del.empty());
      |                          ^~~
./Main.cpp: In function ‘int main()’:
./Main.cpp:94:7: warning: unused variable ‘s’ [-Wunused-variable]
   94 |   int s = 0;
      |       ^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 1300 / 1300
Status
AC × 4
AC × 55
Set Name Test Cases
Sample 00-sample-001.txt, 00-sample-002.txt, 00-sample-003.txt, 00-sample-004.txt
All 00-sample-001.txt, 00-sample-002.txt, 00-sample-003.txt, 00-sample-004.txt, 01-001.txt, 01-002.txt, 01-003.txt, 01-004.txt, 01-005.txt, 01-006.txt, 01-007.txt, 01-008.txt, 01-009.txt, 01-010.txt, 01-011.txt, 01-012.txt, 01-013.txt, 01-014.txt, 01-015.txt, 01-016.txt, 01-017.txt, 01-018.txt, 01-019.txt, 01-020.txt, 01-021.txt, 01-022.txt, 01-023.txt, 01-024.txt, 01-025.txt, 01-026.txt, 01-027.txt, 01-028.txt, 01-029.txt, 01-030.txt, 01-031.txt, 01-032.txt, 01-033.txt, 01-034.txt, 01-035.txt, 01-036.txt, 01-037.txt, 01-038.txt, 01-039.txt, 01-040.txt, 01-041.txt, 01-042.txt, 01-043.txt, 01-044.txt, 01-045.txt, 01-046.txt, 01-047.txt, 01-048.txt, 01-049.txt, 01-050.txt, 01-051.txt
Case Name Status Exec Time Memory
00-sample-001.txt AC 8 ms 3560 KiB
00-sample-002.txt AC 2 ms 3568 KiB
00-sample-003.txt AC 3 ms 3668 KiB
00-sample-004.txt AC 2 ms 3492 KiB
01-001.txt AC 908 ms 3904 KiB
01-002.txt AC 32 ms 3756 KiB
01-003.txt AC 4 ms 3564 KiB
01-004.txt AC 613 ms 3688 KiB
01-005.txt AC 596 ms 3860 KiB
01-006.txt AC 394 ms 3812 KiB
01-007.txt AC 95 ms 3608 KiB
01-008.txt AC 4 ms 3712 KiB
01-009.txt AC 522 ms 3796 KiB
01-010.txt AC 230 ms 3792 KiB
01-011.txt AC 136 ms 3776 KiB
01-012.txt AC 25 ms 3564 KiB
01-013.txt AC 104 ms 3568 KiB
01-014.txt AC 367 ms 3668 KiB
01-015.txt AC 10 ms 3596 KiB
01-016.txt AC 17 ms 3628 KiB
01-017.txt AC 133 ms 3740 KiB
01-018.txt AC 1241 ms 3916 KiB
01-019.txt AC 366 ms 3688 KiB
01-020.txt AC 872 ms 3668 KiB
01-021.txt AC 882 ms 3696 KiB
01-022.txt AC 930 ms 3764 KiB
01-023.txt AC 619 ms 3808 KiB
01-024.txt AC 1035 ms 3736 KiB
01-025.txt AC 487 ms 3780 KiB
01-026.txt AC 666 ms 3808 KiB
01-027.txt AC 748 ms 3624 KiB
01-028.txt AC 1074 ms 3844 KiB
01-029.txt AC 517 ms 3700 KiB
01-030.txt AC 764 ms 3696 KiB
01-031.txt AC 805 ms 3628 KiB
01-032.txt AC 1077 ms 3672 KiB
01-033.txt AC 524 ms 3644 KiB
01-034.txt AC 784 ms 3796 KiB
01-035.txt AC 1237 ms 3836 KiB
01-036.txt AC 367 ms 3812 KiB
01-037.txt AC 872 ms 3876 KiB
01-038.txt AC 905 ms 3620 KiB
01-039.txt AC 924 ms 3676 KiB
01-040.txt AC 610 ms 3780 KiB
01-041.txt AC 1038 ms 3844 KiB
01-042.txt AC 475 ms 3808 KiB
01-043.txt AC 689 ms 3696 KiB
01-044.txt AC 752 ms 3776 KiB
01-045.txt AC 1073 ms 3660 KiB
01-046.txt AC 515 ms 3680 KiB
01-047.txt AC 759 ms 3836 KiB
01-048.txt AC 813 ms 3804 KiB
01-049.txt AC 1069 ms 3840 KiB
01-050.txt AC 535 ms 3640 KiB
01-051.txt AC 814 ms 3616 KiB