QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#374008#7410. Apollonian Networkbear0131WA 4ms69516kbC++147.9kb2024-04-02 10:52:472024-04-02 10:52:49

Judging History

你现在查看的是最新测评结果

  • [2024-04-02 10:52:49]
  • 评测
  • 测评结果:WA
  • 用时:4ms
  • 内存:69516kb
  • [2024-04-02 10:52:47]
  • 提交

answer

#include<bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef array<int, 3> ai3;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int Mod = 1e9 + 7;
inline int sign(int a){ return (a&1) ? (Mod-1) : 1; }
inline void uadd(int &a, int b){ a += b-Mod; a += (a>>31) & Mod; }
inline void usub(int &a, int b){ a -= b, a += (a>>31) & Mod; }
inline void umul(int &a, int b){ a = (int)(1ll * a * b % Mod); }
inline int add(int a, int b){ a += b-Mod; a += (a>>31) & Mod; return a; }
inline int sub(int a, int b){ a -= b, a += (a>>31) & Mod; return a; }
inline int mul(int a, int b){ a = (int)(1ll * a * b % Mod); return a; }
int qpow(int b, ll p){ int ret = 1; while(p){ if(p&1) umul(ret, b); umul(b, b), p >>= 1; } return ret; }
const int fN = 66666;
int fact[fN], invfact[fN], inv[fN];
void initfact(int n){
	fact[0] = 1; for(int i = 1; i <= n; ++i) fact[i] = mul(fact[i-1], i);
	invfact[n] = qpow(fact[n], Mod-2); for(int i = n; i > 0; --i) invfact[i-1] = mul(invfact[i], i);
	for(int i = 1; i <= n; ++i) inv[i] = mul(invfact[i], fact[i-1]);
}
int binom(int n, int m){ return (m < 0 || m > n) ? 0 : mul(fact[n], mul(invfact[m], invfact[n-m])); }
const double pi = acos(-1);
inline void chmax(int &a, int b){ (b>a) ? (a=b) : 0; }
inline void chmin(int &a, int b){ (b<a) ? (a=b) : 0; }
inline void chmax(ll &a, ll b){ (b>a) ? (a=b) : 0; }
inline void chmin(ll &a, ll b){ (b<a) ? (a=b) : 0; }

int n, w[255][255], cid[255][255][255], cc = 0;
array<int, 3> cs[1010];
vi to[1010];
int fa[1010];
int dp0[1010][3], dp1[1010][3][2], go[1010][3];
void dfs0(int id){
	//cout << "dfs0 " << id << " " << fa[id] << endl;
	array<int, 3> c = cs[id];
	for(auto u : to[id]){
		if(u == fa[id]) continue;
		int t;
		t = cid[c[0]][c[1]][u], fa[t] = c[2], dfs0(t);
		t = cid[c[1]][c[2]][u], fa[t] = c[0], dfs0(t);
		t = cid[c[2]][c[0]][u], fa[t] = c[1], dfs0(t);
	}
}

int getdp1(int s, int t, int r, bool sp = 0);

int getdp0(int s, int t, int r){
	int id = cid[s][t][r];
	array<int, 3> c = cs[id];
	int sid = 0, tid = 0;
	while(c[sid] != s) ++sid;
	while(c[tid] != t) ++tid;
	if(sid > tid) swap(s, t), swap(sid, tid);
	int dpj = (sid == 0 ? tid-1 : 2);
	int &ans = dp0[id][dpj];
	if(ans >= 0) return ans;
	ans = w[s][t];
	for(auto u : to[id]){
		if(u == fa[id]) continue;
		chmax(ans, getdp1(s, t, u));
		chmax(ans, max(getdp0(s, u, r), w[s][u]) + max(getdp0(u, t, r), w[u][t]));
		chmax(ans, max(getdp0(s, u, r), w[s][u]) + max(getdp0(u, t, s), w[u][t]));
		chmax(ans, max(getdp0(s, u, t), w[s][u]) + max(getdp0(u, t, r), w[u][t]));
	}
	//cout << "getdp0 " << s << " " << t << " " << r << " = " << ans << endl;
	return ans;
}

int getgo(int s, int t, int r){
	int id = cid[s][t][r];
	array<int, 3> c = cs[id];
	int sid = 0;
	while(c[sid] != s) ++sid;
	int &ans = go[id][sid];
	if(ans >= 0) return ans;

	int u = -1;
	for(auto v : to[id]){
		if(v == fa[id]) continue;
		u = v;
	}

	if(u < 0) return ans = 0;

	ans = w[s][u];
	chmax(ans, max(getgo(s, t, u), getgo(s, r, u)));
	chmax(ans, getdp0(s, u, t) + max(getgo(u, s, r), getgo(u, t, r)));
	chmax(ans, getdp0(s, u, r) + max(getgo(u, s, t), getgo(u, t, r)));
	//cout << "getgo " << s << " " << t << " " << r << " = " << ans << endl;
	return ans;
}

int getdp1(int s, int t, int r, bool sp){
	int id = cid[s][t][r];
	array<int, 3> c = cs[id];
	int sid = 0, tid = 0;
	while(c[sid] != s) ++sid;
	while(c[tid] != t) ++tid;
	if(sid > tid) swap(s, t), swap(sid, tid);
	int dpj = (sid == 0 ? tid-1 : 2);
	int &ans = dp1[id][dpj][sp];
	if(ans >= 0) return ans;

	vi ps = (vi){cs[id][0], cs[id][1], cs[id][2]};
	for(auto u : to[id]){
		if(u == fa[id]) continue;
		ps.push_back(u);
	}

	//cout << "-------------------getdp1 " << s << " " << t << " " << r << "----------------" << endl;

	if((int)ps.size() == 3){
		if(sp){
			ans = w[s][t] + w[t][r] + w[r][s] - min({w[s][t], w[t][r], w[r][s]});
		} else {
			ans = max(w[s][t], w[s][r] + w[r][t]);
		}
		return ans;
	}

	if(sp){
		chmax(ans, getdp1(ps[0], ps[1], ps[3], 1));
		chmax(ans, getdp1(ps[1], ps[2], ps[3], 1));
		chmax(ans, getdp1(ps[2], ps[0], ps[3], 1));
	}

	array< array< array<int, 4>, 8 >, 16 > dp;
	rep(i, 16) rep(j, 8) rep(k, 4) dp[i][j][k] = -inf;
	dp[1<<sid][0][sid] = 0;
	if(sp){
		rep(i, 4) dp[1<<i][0][i] = 0;
		rep(nid, 3){
			int u = nid, v = (nid+1) % 3;
			chmax(dp[1<<v][1<<nid][v], getgo(ps[u], ps[v], ps[3]));
			chmax(dp[1<<u][1<<nid][u], getgo(ps[v], ps[u], ps[3]));
			chmax(dp[1<<3][1<<nid][3], getgo(ps[u], ps[3], ps[v]));
		}
	}
	rep(mask, 16) rep(ts, 8) rep(i, 4) if(dp[mask][ts][i] >= 0) rep(j, 4) if(!((mask>>j) & 1)){
		//cout << s << t << r << " | " << (bitset<4>)mask << " " << (bitset<3>)ts << " " << i << ": " << dp[mask][ts][i] << endl;
		chmax(dp[mask | (1<<j)][ts][j], dp[mask][ts][i] + w[ps[i]][ps[j]]);
		int u = i, v = j;
		if(u > v) swap(u, v);
		if(v != 3){
			int nid = (u == 0 && v == 2) ? 2 : u;
			if(!((ts>>nid) & 1)){
				chmax(dp[mask | (1<<j)][ts | (1<<nid)][j], dp[mask][ts][i] + getdp0(ps[u], ps[v], ps[3]));
				if(!((mask>>3) & 1))
					chmax(dp[mask | (1<<j) | (1<<3)][ts | (1<<nid)][j], dp[mask][ts][i] + getdp1(ps[u], ps[v], ps[3]));
			}
		} else {
			int nid = u, ru = (nid+1) % 3;
			if(!((ts>>nid) & 1)){
				chmax(dp[mask | (1<<j)][ts | (1<<nid)][j], dp[mask][ts][i] + getdp0(ps[u], ps[v], ps[ru]));
				if(!((mask>>ru) & 1))
					chmax(dp[mask | (1<<j) | (1<<ru)][ts | (1<<nid)][j], dp[mask][ts][i] + getdp1(ps[u], ps[v], ps[ru]));
			}
			nid = (u+2) % 3, ru = nid;
			//cout << nid << " " << ru << endl;
			if(!((ts>>nid) & 1)){
				chmax(dp[mask | (1<<j)][ts | (1<<nid)][j], dp[mask][ts][i] + getdp0(ps[u], ps[v], ps[ru]));
				if(!((mask>>ru) & 1)){
					chmax(dp[mask | (1<<j) | (1<<ru)][ts | (1<<nid)][j], dp[mask][ts][i] + getdp1(ps[u], ps[v], ps[ru]));
				}
			}
		}
	}
	//rep(mask, 16) rep(ts, 8) rep(i, 4) if(dp[mask][ts][i] >= 0)
	//	cout << "? " << s << t << r << " | " << (bitset<4>)mask << " " << (bitset<3>)ts << " " << i << ": " << dp[mask][ts][i] << endl;

	ans = 0;
	rep(mask, 16) rep(ts, 8) chmax(ans, dp[mask][ts][tid]);
	if(sp){
		rep(mask, 16) rep(ts, 8) rep(i, 4) chmax(ans, dp[mask][ts][i]);
		rep(mask, 16) rep(ts, 8) rep(nid, 3) if(!((ts>>nid) & 1)){
			int u = nid, v = (nid+1) % 3;
			chmax(ans, dp[mask][ts][u] + getgo(ps[u], ps[v], ps[3]));
			chmax(ans, dp[mask][ts][v] + getgo(ps[v], ps[u], ps[3]));
			chmax(ans, dp[mask][ts][3] + getgo(ps[3], ps[v], ps[u]));
		}
	}
	//cout << "! " << ans << endl;
	return ans;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	cin >> n;
	memset(w, 0x3f, sizeof(w));
	rep(i, 3*(n-2)){
		int u, v, c;
		cin >> u >> v >> c;
		--u, --v;
		w[u][v] = w[v][u] = c;
	}

	if(n == 3){
		cout << w[0][1] + w[1][2] + w[2][0] - min({w[0][1], w[1][2], w[2][0]}) << "\n";
		return 0;
	}

	memset(cid, -1, sizeof(cid));
	rep(i, n) rep(j, i) if(w[i][j] < inf) rep(k, j) if(w[j][k] < inf && w[k][i] < inf){
		cs[cc] = {k, j, i};
		cid[i][j][k] = cid[i][k][j] = cid[j][i][k] = cid[j][k][i] = cid[k][i][j] = cid[k][j][i] = cc++;
	}

	int rt = 0;
	rep(i, cc){
		array<int, 3> c = cs[i];
		rep(u, n) if(cid[c[0]][c[1]][u] >= 0 && cid[c[1]][c[2]][u] >= 0 && cid[c[2]][c[0]][u] >= 0) to[i].push_back(u);
		if((int)to[i].size() == 1) rt = i;
	}

	//rep(i, n) rep(j, n) rep(k, n) cout << i << " " << j << " " << k << ": " << cid[i][j][k] << endl;

	//rep(i, cc) cout << i << ": " << cs[i][0] << " " << cs[i][1] << " " << cs[i][2] << endl;
	//rep(i, cc){
	//	cout << i << " to ";
	//	for(auto u : to[i]) cout << u << " ";
	//	cout << endl;
	//}
	//cout << rt << endl;

	fa[rt] = -1, dfs0(rt);

	memset(dp0, 0xc0, sizeof(dp0)), memset(dp1, 0xc0, sizeof(dp1)), memset(go, 0xc0, sizeof(go));
	
	cout << getdp1(cs[rt][0], cs[rt][1], cs[rt][2], 1) << "\n";

	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 6000kb

input:

3
1 2 1
2 3 1
3 1 2

output:

3

result:

ok 1 number(s): "3"

Test #2:

score: 0
Accepted
time: 4ms
memory: 69516kb

input:

10
1 2 4
2 3 4
3 1 3
6 1 3
6 2 3
6 3 4
4 6 4
4 3 4
4 2 3
5 1 3
5 6 3
5 2 4
10 1 4
10 3 3
10 6 3
7 1 4
7 10 4
7 6 3
8 1 3
8 3 4
8 10 4
9 3 4
9 8 3
9 10 3

output:

35

result:

ok 1 number(s): "35"

Test #3:

score: 0
Accepted
time: 1ms
memory: 5692kb

input:

3
1 2 0
3 2 1
3 1 1

output:

2

result:

ok 1 number(s): "2"

Test #4:

score: 0
Accepted
time: 1ms
memory: 3852kb

input:

3
3 2 123
2 1 67
3 1 763

output:

886

result:

ok 1 number(s): "886"

Test #5:

score: 0
Accepted
time: 0ms
memory: 5984kb

input:

3
3 2 724482
2 1 326757
3 1 386076

output:

1110558

result:

ok 1 number(s): "1110558"

Test #6:

score: 0
Accepted
time: 4ms
memory: 69500kb

input:

4
4 2 1
2 3 0
3 4 1
1 4 1
1 2 1
1 3 1

output:

3

result:

ok 1 number(s): "3"

Test #7:

score: 0
Accepted
time: 0ms
memory: 68612kb

input:

4
3 4 831
4 2 412
3 2 641
1 3 756
1 4 909
2 1 254

output:

2381

result:

ok 1 number(s): "2381"

Test #8:

score: 0
Accepted
time: 4ms
memory: 68660kb

input:

4
2 4 381962
4 3 793293
2 3 408508
2 1 425132
1 4 774548
3 1 430802

output:

1992973

result:

ok 1 number(s): "1992973"

Test #9:

score: 0
Accepted
time: 3ms
memory: 68956kb

input:

5
2 5 1
3 5 1
2 3 1
4 2 1
4 5 1
3 4 0
1 2 1
4 1 0
1 3 0

output:

4

result:

ok 1 number(s): "4"

Test #10:

score: 0
Accepted
time: 0ms
memory: 68908kb

input:

5
2 5 595
5 4 813
4 2 828
2 1 852
5 1 959
1 4 816
3 2 112
3 1 252
3 4 424

output:

3063

result:

ok 1 number(s): "3063"

Test #11:

score: 0
Accepted
time: 4ms
memory: 69288kb

input:

5
4 3 200019
2 4 422357
3 2 853305
3 1 371136
4 1 358800
2 1 889385
3 5 390874
1 5 239492
4 5 317497

output:

2492364

result:

ok 1 number(s): "2492364"

Test #12:

score: 0
Accepted
time: 0ms
memory: 68868kb

input:

6
1 5 1
3 5 0
3 1 0
2 1 0
5 2 1
2 3 0
4 1 1
4 2 1
5 4 1
1 6 1
4 6 1
2 6 1

output:

4

result:

ok 1 number(s): "4"

Test #13:

score: 0
Accepted
time: 0ms
memory: 69228kb

input:

6
1 4 735
3 4 627
3 1 442
2 1 496
2 4 325
3 2 836
1 5 637
5 2 53
3 5 32
6 2 684
5 6 448
3 6 113

output:

3519

result:

ok 1 number(s): "3519"

Test #14:

score: 0
Accepted
time: 0ms
memory: 68880kb

input:

6
4 3 205961
5 4 30950
5 3 65968
2 3 303988
2 4 184729
2 5 843062
4 6 481378
6 2 664463
5 6 545405
1 3 383865
2 1 307891
4 1 669247

output:

3042015

result:

ok 1 number(s): "3042015"

Test #15:

score: 0
Accepted
time: 0ms
memory: 69108kb

input:

7
4 5 1
3 5 0
4 3 0
4 7 1
5 7 0
3 7 1
1 5 1
7 1 0
3 1 1
4 2 1
7 2 1
2 5 1
6 4 1
6 2 1
7 6 1

output:

6

result:

ok 1 number(s): "6"

Test #16:

score: -100
Wrong Answer
time: 0ms
memory: 68756kb

input:

7
6 5 862
6 4 299
4 5 584
5 2 737
6 2 957
2 4 407
7 6 260
2 7 857
7 4 631
2 3 701
3 7 361
3 4 541
1 5 283
2 1 228
1 6 163

output:

3975

result:

wrong answer 1st numbers differ - expected: '4131', found: '3975'