QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#374455 | #7410. Apollonian Network | bear0131 | TL | 128ms | 72916kb | C++14 | 8.0kb | 2024-04-02 14:22:31 | 2024-04-02 14:22:32 |
Judging History
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 rt, fa[1010];
int dp[1010][2][2][2][111];
int outmp[66666];
void dfs0(int id){
//cout << "dfs0 " << id << " " << fa[id] << "\n";
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);
}
}
const int outp[4] = {0, 1, 2, 7}, outq[8] = {0, 1, 2, -1, -1, -1, -1, 3};
int getdp(int id, array<int, 3> tp, int outs){
int DEBUG = 0;//(id == 12 && outs == 16384);
if(DEBUG){
cout << "------------------getdp " << id << " {" << tp[0] << " " << tp[1] << " " << tp[2] << "} " << outs << "-------------------------" << "\n";
cout << cs[id][0] << " " << cs[id][1] << " " << cs[id][2] << "\n";
rep(i, 4) rep(j, 4) if((outs>>(i*4+j)) & 1){ cout << i << " " << j << "\n"; }
}
int outid = outmp[outs];
int &ans = dp[id][tp[0]][tp[1]][tp[2]][outid];
if(ans >= 0) return ans;
vi ps(3);
rep(i, 3) ps[i] = cs[id][i];
int m = -1;
for(auto v : to[id]){
if(v == fa[id]) continue;
m = v;
ps.push_back(m);
}
int cn = 8;
array< array<int, 8>, 8 > has, done, ew;
rep(i, cn) rep(j, cn) has[i][j] = done[i][j] = ew[i][j] = 0;
auto ae = [&](int u, int v, bool hasw = 0){
if(DEBUG) cout << "ae " << u << " " << v << "\n";
has[u][v] = has[v][u] = 1;
if(hasw) ew[u][v] = ew[v][u] = w[ps[u]][ps[v]];
};
if(id == rt){
ae(0, 1, 1), ae(1, 2, 1), ae(2, 0, 1);
}
if(m >= 0){
ae(0, 3, 1), ae(1, 3, 1), ae(2, 3, 1), ae(7, 3);
ae(0, 4), ae(1, 4), ae(3, 4), ae(7, 4);
ae(1, 5), ae(2, 5), ae(3, 5), ae(7, 5);
ae(2, 6), ae(0, 6), ae(3, 6), ae(7, 6);
}
rep(i, 4) rep(j, 4){
if((outs>>(i*4+j)) & 1){
has[outp[i]][outp[j]] = 1;
if(i == 3) rep(k, 7) if(k != j) has[7][k] = 0;
if(j == 3) rep(k, 7) if(k != i) has[k][7] = 0;
}
}
if(DEBUG){
rep(i, 8){
rep(j, 8) cout << has[i][j] << " ";
cout << "\n";
}
}
vi pth;
auto check = [&](){
int ok = 1;
rep(i, 4){
rep(j, 4){
if((outs>>(i*4+j)) & 1){
if(!done[outp[i]][outp[j]]){
ok = 0;
break;
}
}
}
if(!ok) break;
}
if(!ok) return ;
int val = 0;
array<int, 8> lstout;
rep(i, 8) lstout[i] = -1;
array< vector<pii>, 8 > es;
rep(i, 8) es[i].clear();
rep(i, (int)pth.size() - 1){
int u = pth[i], v = pth[i+1];
if(u <= 3 && v <= 3){
val += ew[u][v];
}
if(v >= 4 && v <= 6){
if(lstout[v] < 0)
es[v].push_back(make_pair(7, u));
else
es[v].push_back(make_pair(lstout[v], u));
}
lstout[u] = v;
}
DEBUG = 0;//((id == 12 && outs == 16384) && pth == (vi){7, 2, 3, 4, 0, 1, 4, 7});
if(DEBUG){
for(auto u : pth) cout << u << ", ";
cout << "\n";
}
for(int i = 4; i <= 6; ++i){
if(es[i].empty()) continue;
es[i].push_back(make_pair(lstout[i], 7));
int nid = cid[ps[i-4]][ps[(i-4+1)%3]][ps[3]];
static int can[255], mp[255];
rep(j, 3) mp[cs[nid][j]] = j;
rep(j, 4) can[ps[j]] = (done[i][j] || done[j][i]);
array<int, 3> ntp;
rep(j, 3) ntp[j] = can[cs[nid][j]];
int nmask = 0;
for(auto p : es[i]){
if(p.first == p.second) continue;
//cout << p.first << " to " << p.second << "\n";
nmask |= 1 << (outq[p.first == 7 ? 7 : mp[ps[p.first]]] * 4 + outq[p.second == 7 ? 7 : mp[ps[p.second]]]);
}
if(DEBUG) cout << val << endl;
val += getdp(nid, ntp, nmask);
//if(val == 5570866) cout << "from " << i << " " << tmptmp << endl;
if(val < 0) break;
}
//if(val == 5570866 || val == 5570866 - 1652412){
// cout << "------------------getdp " << id << " {" << tp[0] << " " << tp[1] << " " << tp[2] << "} " << outs << "-------------------------" << "\n";
// cout << cs[id][0] << " " << cs[id][1] << " " << cs[id][2] << "\n";
// rep(i, 4) rep(j, 4) if((outs>>(i*4+j)) & 1){ cout << i << " " << j << "\n"; }
// for(auto u : pth) cout << u << ", ";
// cout << "\n";
//}
chmax(ans, val);
};
array<int, 8> vis;
rep(i, 8) vis[i] = 0;
function<void(int)> dfs = [&](int u){
++vis[u], pth.push_back(u);
if((int)pth.size() > 1 && u == 7){
check();
--vis[u], pth.pop_back();
return ;
}
rep(t, cn) if(has[u][t]){
//if(DEBUG) cout << " dfs " << u << " to " << t << "\n";
if(done[u][t] || (t < 3 && tp[t] == 0) || (t <= 3 && vis[t])) continue;
done[u][t] = 1;
dfs(t);
done[u][t] = 0;
}
--vis[u], pth.pop_back();
};
dfs(7);
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++;
}
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] << "\n";
//rep(i, cc) cout << i << ": " << cs[i][0] << " " << cs[i][1] << " " << cs[i][2] << "\n";
//rep(i, cc){
// cout << cs[i][0] << cs[i][1] << cs[i][2] << " " << i << " to ";
// for(auto u : to[i]) cout << u << " ";
// cout << "\n";
//}
//cout << rt << "\n";
fa[rt] = -1, dfs0(rt);
memset(dp, 0xc0, sizeof(dp));
int co = 0;
rep(mask, 1<<16){
array< array<int, 4>, 4 > has;
rep(i, 4) rep(j, 4) has[i][j] = (mask>>(i*4+j)) & 1;
bool ok = 1;
rep(i, 4) if(has[i][i]) ok = 0;
rep(i, 4){
int cnt = 0;
rep(j, 4) cnt += has[i][j];
if(cnt > 1) ok = 0;
}
rep(i, 4){
int cnt = 0;
rep(j, 4) cnt += has[j][i];
if(cnt > 1) ok = 0;
}
if(ok) outmp[mask] = co++;
}
//cout << co << "\n";
int ans = getdp(rt, {1, 1, 1}, 0);
rep(i, 3) chmax(ans, getdp(rt, {1, 1, 1}, 1 << (i*4 + 3)));
rep(j, 3) chmax(ans, getdp(rt, {1, 1, 1}, 1 << (3*4 + j)));
rep(i, 3) rep(j, 3)
chmax(ans, getdp(rt, {1, 1, 1}, (1 << (i*4 + 3)) + (1 << (3*4 + j))));
cout << ans << "\n";
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 5916kb
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: 54ms
memory: 72676kb
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: 0ms
memory: 7764kb
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: 6140kb
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: 1ms
memory: 5864kb
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: 72396kb
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: 10ms
memory: 72200kb
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: 72348kb
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: 29ms
memory: 72160kb
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: 29ms
memory: 72172kb
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: 26ms
memory: 72164kb
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: 41ms
memory: 72300kb
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: 41ms
memory: 72440kb
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: 38ms
memory: 72384kb
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: 29ms
memory: 72168kb
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: 0
Accepted
time: 27ms
memory: 72480kb
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:
4131
result:
ok 1 number(s): "4131"
Test #17:
score: 0
Accepted
time: 19ms
memory: 72216kb
input:
7 5 2 459322 6 5 427540 6 2 187367 2 4 961056 5 4 103338 4 6 983153 2 1 771767 1 4 580227 6 1 371949 5 3 623838 4 3 208838 3 6 578640 4 7 222571 1 7 669259 7 6 53303
output:
4587713
result:
ok 1 number(s): "4587713"
Test #18:
score: 0
Accepted
time: 27ms
memory: 72204kb
input:
8 4 7 1 4 3 1 7 3 0 7 8 1 8 4 1 8 3 1 4 1 1 1 8 1 3 1 1 5 7 1 8 5 0 5 4 1 2 8 0 1 2 0 3 2 0 8 6 0 6 5 1 6 4 1
output:
6
result:
ok 1 number(s): "6"
Test #19:
score: 0
Accepted
time: 89ms
memory: 72916kb
input:
8 3 7 367 6 7 119 6 3 702 1 3 769 1 7 948 6 1 515 7 8 96 1 8 55 6 8 642 5 7 564 5 8 195 5 1 54 4 7 934 4 5 641 4 1 671 1 2 186 8 2 70 6 2 325
output:
4706
result:
ok 1 number(s): "4706"
Test #20:
score: 0
Accepted
time: 29ms
memory: 72208kb
input:
8 7 3 728075 1 3 669805 1 7 908111 7 2 901659 2 3 127509 2 1 48044 8 7 287910 2 8 584282 8 3 292959 6 2 641410 8 6 979321 6 3 136905 7 4 212548 2 4 489178 4 1 379769 7 5 350931 5 4 21571 1 5 597009
output:
4636063
result:
ok 1 number(s): "4636063"
Test #21:
score: 0
Accepted
time: 23ms
memory: 72172kb
input:
9 8 5 0 1 5 0 8 1 0 8 6 0 6 5 0 6 1 1 3 8 1 6 3 1 1 3 1 4 8 1 3 4 1 1 4 0 7 8 0 7 4 0 3 7 1 4 2 0 7 2 1 3 2 1 4 9 0 9 2 1 3 9 0
output:
6
result:
ok 1 number(s): "6"
Test #22:
score: 0
Accepted
time: 94ms
memory: 72592kb
input:
9 8 7 159 2 7 324 8 2 134 8 5 469 7 5 329 5 2 259 1 7 670 5 1 765 1 2 740 8 3 116 5 3 527 3 2 723 8 9 956 9 5 604 9 7 872 5 4 124 3 4 340 4 2 86 8 6 984 3 6 926 5 6 201
output:
6090
result:
ok 1 number(s): "6090"
Test #23:
score: 0
Accepted
time: 47ms
memory: 72412kb
input:
9 2 9 1776 8 2 965915 9 8 637994 5 9 391975 5 2 576116 5 8 303357 7 9 805273 5 7 721534 8 7 639873 9 3 249910 7 3 376286 3 5 819176 9 4 773174 4 3 172900 7 4 185855 6 9 268943 6 5 9090 2 6 187666 1 3 745755 1 4 911957 1 7 476567
output:
5848789
result:
ok 1 number(s): "5848789"
Test #24:
score: 0
Accepted
time: 77ms
memory: 72420kb
input:
10 4 8 1 4 2 0 2 8 1 3 8 1 3 4 0 2 3 1 7 8 1 7 3 1 7 4 0 8 5 1 3 5 1 5 2 1 4 1 1 1 3 0 2 1 0 8 6 1 6 7 0 6 3 0 10 8 1 5 10 1 3 10 1 8 9 0 7 9 1 4 9 1
output:
9
result:
ok 1 number(s): "9"
Test #25:
score: 0
Accepted
time: 57ms
memory: 72148kb
input:
10 8 4 0 6 8 0 4 6 0 4 2 0 2 8 1 6 2 1 4 9 1 2 9 0 9 8 0 2 1 1 9 1 0 1 8 0 4 3 1 3 9 1 3 8 1 4 5 0 3 5 1 9 5 0 4 7 1 7 2 1 7 6 0 2 10 1 1 10 1 10 9 1
output:
8
result:
ok 1 number(s): "8"
Test #26:
score: 0
Accepted
time: 74ms
memory: 72196kb
input:
10 1 10 0 2 10 1 2 1 0 1 5 0 10 5 0 5 2 0 10 3 0 3 5 0 3 2 1 6 5 1 3 6 0 2 6 1 3 9 1 9 6 0 9 2 1 3 4 1 4 9 0 4 6 0 8 9 0 4 8 1 6 8 0 7 4 0 7 8 1 6 7 0
output:
7
result:
ok 1 number(s): "7"
Test #27:
score: 0
Accepted
time: 111ms
memory: 72472kb
input:
10 5 8 771 9 5 728 9 8 240 2 8 815 2 5 118 9 2 988 10 8 513 2 10 949 10 5 513 6 8 746 6 2 478 9 6 332 7 5 754 7 2 817 9 7 354 2 4 196 10 4 904 5 4 794 8 1 153 10 1 339 1 5 410 3 8 256 10 3 155 2 3 770
output:
5930
result:
ok 1 number(s): "5930"
Test #28:
score: 0
Accepted
time: 94ms
memory: 72148kb
input:
10 7 2 535 4 2 494 7 4 642 3 7 446 2 3 339 3 4 949 1 7 644 1 3 810 1 2 466 8 7 42 8 1 297 8 2 118 9 7 947 8 9 647 2 9 855 7 10 758 8 10 193 10 1 56 5 8 185 10 5 699 5 1 483 7 6 593 6 1 415 3 6 553
output:
6288
result:
ok 1 number(s): "6288"
Test #29:
score: 0
Accepted
time: 40ms
memory: 72340kb
input:
10 3 8 760 3 10 881 10 8 600 7 8 732 3 7 773 10 7 567 9 3 192 9 7 798 9 10 427 3 6 38 9 6 562 10 6 757 4 9 325 6 4 748 4 10 427 5 6 883 5 4 532 5 10 90 1 4 618 5 1 447 1 10 117 4 2 989 2 1 923 10 2 578
output:
7255
result:
ok 1 number(s): "7255"
Test #30:
score: 0
Accepted
time: 128ms
memory: 72508kb
input:
10 9 3 532820 7 9 884172 3 7 990335 1 3 674226 1 9 748597 1 7 720815 3 2 852207 1 2 360164 9 2 939116 8 3 967366 1 8 714210 7 8 408588 9 10 789199 1 10 242778 10 7 442389 3 5 790845 5 2 622752 5 1 116990 3 4 904891 2 4 469074 9 4 646557 6 1 28022 6 2 348749 9 6 363364
output:
6100738
result:
ok 1 number(s): "6100738"
Test #31:
score: 0
Accepted
time: 59ms
memory: 72172kb
input:
10 1 5 91842 5 10 353811 10 1 62948 2 1 198100 2 5 513247 10 2 916235 4 1 783495 4 2 162627 5 4 778519 2 6 332497 6 4 592563 5 6 250810 3 1 392794 4 3 782688 3 5 722 7 2 123632 7 6 49788 4 7 765664 8 2 771323 7 8 937893 8 4 407488 9 5 772069 9 2 228947 10 9 514461
output:
6104744
result:
ok 1 number(s): "6104744"
Test #32:
score: 0
Accepted
time: 74ms
memory: 72168kb
input:
10 7 5 369618 5 2 111605 2 7 560430 7 1 53231 1 5 597492 2 1 656016 8 5 236755 1 8 146229 8 2 55028 1 3 955478 8 3 425707 2 3 342498 8 10 975337 3 10 483824 2 10 65867 9 8 412169 10 9 183571 3 9 646463 6 8 911293 6 9 178083 6 3 470761 8 4 493752 4 6 785212 4 3 821348
output:
6218303
result:
ok 1 number(s): "6218303"
Test #33:
score: -100
Time Limit Exceeded
input:
250 109 16 1 132 109 1 132 16 1 127 16 0 127 109 0 127 132 0 133 16 1 133 127 1 133 109 0 124 16 1 127 124 0 124 132 1 41 109 1 41 127 1 132 41 0 44 16 0 44 133 1 127 44 1 121 16 0 121 133 0 121 109 1 127 48 0 48 133 1 48 109 0 16 204 1 204 124 1 204 127 0 16 236 1 124 236 0 132 236 1 17 127 1 124 1...