QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#626880 | #6437. Paimon's Tree | juan_123 | WA | 311ms | 22228kb | C++14 | 3.2kb | 2024-10-10 13:41:06 | 2024-10-10 13:41:08 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define double long double
#define lowbit(x) (x&(-x))
#define int long long
const int inf = 10000000000000000;
int val[155][155];
vector<int>p[155];
int a[155],sz[155],vis[155],f[155];
int dp[155][155][155][2][2],ok[155],dis[155][155];
bool in[155][155][155];
int n;
void dfs(int now,int fa){f[now]=fa;for(auto x:p[now])if(x!=fa)dfs(x,now);}
void dfs1(int now,int fa,int& a){
if(!ok[now])++a;
for(auto x:p[now]){if(x!=fa and !ok[x])dfs1(x,now,a);}
}
bool cmp(pair<int,int>a,pair<int,int>b){return dis[a.first][a.second]<dis[b.first][b.second];}
void solve(){
cin >> n;++n;
for(int i = 1;i<n;i++)cin >> a[i];
// cout << 111 << endl;
for(int i =0;i<=n;i++)p[i].clear();
for(int i =0;i<=n;i++)for(int j =0;j<=n;j++)for(int k = 0;k<=n;k++)in[i][j][k] = 0;
for(int i= 1;i<=n;i++)for(int j =0;j<=n;j++)for(int k =0;k<=n;k++){
for(int l =0;l<2;l++){
for(int d=0;d<2;d++){
dp[i][j][k][l][d]=-inf;
}
}
}
for(int i =1;i<n;i++){
int a,b;cin >> a >> b;
p[a].push_back(b),p[b].push_back(a);
}
for(int i = 1;i<=n;i++){
dfs(i,i);
vector<int>p;
// for(int j = 1;j<=n;j++)cout << f[j] <<' ';cout << endl;
//continue;
for(int j = 1;j<=n;j++){
p.clear();
int now = j;
while(now!=i){
// cout <<" " << now << endl;
p.push_back(now),now = f[now];
}
p.push_back(now);
dis[i][j]=p.size();
for(int k = 1;k<=n;k++)ok[k] =0 ;
for(auto x:p)ok[x]=1,in[i][j][x]=1;
val[i][j]=dis[i][j];
for(int k = 1;k+1<p.size();k++){
dfs1(p[k],p[k],val[i][j]);
}
}
}
// for(int i = 1;i<=n;i++){
// for(int j = 1;j<=n;j++){
// cout << val[i][j] << " ";
/// }
// cout << endl;
///// }
//return;
//不包含端点的路径和
vector<pair<int,int> >s;
for(int i = 1;i<=n;i++)for(int j = 1;j<=n;j++)s.push_back({i,j});
sort(s.begin(),s.end(),cmp);
for(int i = 1;i<=n;i++)dp[i][i][0][1][1] = 0;
int ans = 0;
for(int ii =0;ii<s.size();ii++){
int x = s[ii].first,y = s[ii].second;
for(int k =0;k<=min(val[x][y],n-1);k++){
ans=max(ans,dp[x][y][k][1][1]);
// cout << " " << x <<" " << y <<" " << k << " " << dp[x][y][k][1][1] << endl;
for(int i =0;i<2;i++){
for(int j =0;j<2;j++){
int v = dp[x][y][k][i][j];
if(i == 1){
//拓展一端
for(int a:p[x]){
if(in[x][y][a])continue;
dp[a][y][k][0][j] = max(dp[a][y][k][0][j],v);
}
}
if(j == 1){
for(int a:p[y]){
if(in[x][y][a])continue;
dp[x][a][k][i][0] = max(dp[x][a][k][i][0],v);
}
}
if(i == 0){
//确定权值
if(k+1<=val[x][y])dp[x][y][k+1][1][j]=max(dp[x][y][k+1][1][j],v+a[k+1]);
}
if(j == 0){
if(k+1<=val[x][y])dp[x][y][k+1][i][1]=max(dp[x][y][k+1][i][1],v+a[k+1]);
}
//往后走一步
if(k+1<=val[x][y])dp[x][y][k+1][i][j]=max(dp[x][y][k+1][i][j],v);
}
}
}
}
cout << ans << endl;
}
signed main(){
// freopen("length.in","r",stdin);
// freopen("length.out","w",stdout);
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int t;cin >> t;
while(t--)solve();
return 0;
}/*
1
4
1 1 5 4
1 2
2 3
2 4
3 5
*/
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 11788kb
input:
2 5 1 7 3 5 4 1 3 2 3 3 4 4 5 4 6 1 1000000000 1 2
output:
16 1000000000
result:
ok 2 number(s): "16 1000000000"
Test #2:
score: -100
Wrong Answer
time: 311ms
memory: 22228kb
input:
5000 19 481199252 336470888 634074578 642802746 740396295 773386884 579721198 396628655 503722503 971207868 202647942 2087506 268792718 46761498 443917727 16843338 125908043 691952768 717268783 9 4 4 18 12 9 10 9 4 1 6 12 2 18 9 13 6 14 4 8 2 3 10 17 2 20 19 20 5 1 12 15 15 16 4 7 17 11 4 240982681 ...
output:
5750811120 1896999359 4208559611 4140156713 5361004844 1875024569 3690026656 3702623113 3412485417 7807375141 5341435147 2355946110 3090496776 5626636202 4729664767 2207592767 572868833 4759005973 2944749369 2538044586 3083947956 5757497518 1421427135 3971435093 1197051728 396588615 251138097 221986...
result:
wrong answer 48th numbers differ - expected: '5317528311', found: '5514819848'