QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#133119#6629. Travelling TraderPCTprobability#0 184ms819032kbC++1412.2kb2023-08-01 15:55:072024-07-04 01:06:30

Judging History

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

  • [2024-07-04 01:06:30]
  • 评测
  • 测评结果:0
  • 用时:184ms
  • 内存:819032kb
  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-08-01 15:55:07]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif
using ll = int;
using ld = long double;
using ull = unsigned long long;
#define endl "\n"
typedef pair<int, int> Pii;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REP3(i, m, n) for (int i = (m); (i) < int(n); ++ (i))
#define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define ALL(x) begin(x), end(x)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(s) (s).begin(),(s).end()
#define drep2(i, m, n) for (int i = (m)-1; i >= (n); --i)
#define drep(i, n) drep2(i, n, 0)
#define rever(vec) reverse(vec.begin(), vec.end())
#define sor(vec) sort(vec.begin(), vec.end())
#define fi first
#define FOR_(n) for (ll _ = 0; (_) < (ll)(n); ++(_))
#define FOR(i, n) for (ll i = 0; (i) < (ll)(n); ++(i))
#define se second
#define pb push_back
#define P pair<ll,ll>
#define PQminll priority_queue<ll, vector<ll>, greater<ll>>
#define PQmaxll priority_queue<ll,vector<ll>,less<ll>>
#define PQminP priority_queue<P, vector<P>, greater<P>>
#define PQmaxP priority_queue<P,vector<P>,less<P>>
#define NP next_permutation
//const ll mod = 1000000009;
//const ll mod = 998244353;
const ll mod = 1000000007;
const ll inf = 4100000000000000000ll;
const ld eps = ld(0.00000000001);
static const long double pi = 3.141592653589793;
template<class T>void vcin(vector<T> &n){for(int i=0;i<int(n.size());i++) cin>>n[i];}
template<class T,class K>void vcin(vector<T> &n,vector<K> &m){for(int i=0;i<int(n.size());i++) cin>>n[i]>>m[i];}
template<class T>void vcout(vector<T> &n){for(int i=0;i<int(n.size());i++){cout<<n[i]<<" ";}cout<<endl;}
template<class T>void vcin(vector<vector<T>> &n){for(int i=0;i<int(n.size());i++){for(int j=0;j<int(n[i].size());j++){cin>>n[i][j];}}}
template<class T>void vcout(vector<vector<T>> &n){for(int i=0;i<int(n.size());i++){for(int j=0;j<int(n[i].size());j++){cout<<n[i][j]<<" ";}cout<<endl;}cout<<endl;}
void yes(bool a){cout<<(a?"yes":"no")<<endl;}
void YES(bool a){cout<<(a?"YES":"NO")<<endl;}
void Yes(bool a){cout<<(a?"Yes":"No")<<endl;}
void possible(bool a){ cout<<(a?"possible":"impossible")<<endl; }
void Possible(bool a){ cout<<(a?"Possible":"Impossible")<<endl; }
void POSSIBLE(bool a){ cout<<(a?"POSSIBLE":"IMPOSSIBLE")<<endl; }
#define FOR_R(i, n) for (ll i = (ll)(n)-1; (i) >= 0; --(i))
template<class T>auto min(const T& a){ return *min_element(all(a)); }
template<class T>auto max(const T& a){ return *max_element(all(a)); }
template<class T,class F>void print(pair<T,F> a){cout<<a.fi<<" "<<a.se<<endl;}
template<class T>bool chmax(T &a,const T b) { if (a<b) { a=b; return 1; } return 0;}
template<class T>bool chmin(T &a,const T b) { if (b<a) { a=b; return 1; } return 0;}
template<class T> void ifmin(T t,T u){if(t>u){cout<<-1<<endl;}else{cout<<t<<endl;}}
template<class T> void ifmax(T t,T u){if(t>u){cout<<-1<<endl;}else{cout<<t<<endl;}}
ll fastgcd(ll u,ll v){ll shl=0;while(u&&v&&u!=v){bool eu=!(u&1);bool ev=!(v&1);if(eu&&ev){++shl;u>>=1;v>>=1;}else if(eu&&!ev){u>>=1;}else if(!eu&&ev){v>>=1;}else if(u>=v){u=(u-v)>>1;}else{ll tmp=u;u=(v-u)>>1;v=tmp;}}return !u?v<<shl:u<<shl;}
ll modPow(ll a, ll n, ll mod) { if(mod==1) return 0;ll ret = 1; ll p = a % mod; while (n) { if (n & 1) ret = ret * p % mod; p = p * p % mod; n >>= 1; } return ret; }
vector<ll> divisor(ll x){ vector<ll> ans; for(ll i = 1; i * i <= x; i++){ if(x % i == 0) {ans.push_back(i); if(i*i!=x){ ans.push_back(x / ans[i]);}}}sor(ans); return ans; }
ll pop(ll x){return __builtin_popcountll(x);}
ll poplong(ll x){ll y=-1;while(x){x/=2;y++;}return y;}
P hyou(P a){ll x=fastgcd(abs(a.fi),abs(a.se));a.fi/=x;a.se/=x;if(a.se<0){a.fi*=-1;a.se*=-1;}return a;}
P Pplus(P a,P b){ return hyou({a.fi*b.se+b.fi*a.se,a.se*b.se});}
P Ptimes(P a,ll b){ return hyou({a.fi*b,a.se});}
P Ptimes(P a,P b){ return hyou({a.fi*b.fi,a.se*b.se});}
P Pminus(P a,P b){ return hyou({a.fi*b.se-b.fi*a.se,a.se*b.se});}
P Pgyaku(P a){ return hyou({a.se,a.fi});}
template<class T>
struct Sum{
  vector<T> data;
  Sum(const vector<T>& v):data(v.size()+1){
    for(ll i=0;i<v.size();i++) data[i+1]=data[i]+v[i];
  }
  T get(ll l,ll r) const {
    return data[r]-data[l];
  }
};
template<class T>
struct Sum2{
  vector<vector<T>> data;
  Sum2(const vector<vector<T>> &v):data(v.size()+1,vector<T>(v[0].size()+1)){
    for(int i=0;i<v.size();i++) for(int j=0;j<v[i].size();j++) data[i+1][j+1]=data[i][j+1]+v[i][j];
    for(int i=0;i<v.size();i++) for(int j=0;j<v[i].size();j++) data[i+1][j+1]+=data[i+1][j];
  }
  T get(ll x1,ll y1,ll x2,ll y2) const {
    return data[x2][y2]+data[x1][y1]-data[x1][y2]-data[x2][y1];
  }
};
 
void cincout(){
  ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
  cout<< fixed << setprecision(15);
}
vector<ll> g[200000];
vector<ll> ans;
ll use[200000];
ll p[200000];
ll dp[200000][2][3];
deque<ll> way[200000][2][3];
int dfs(int a,int b,int x){
  for(auto e:g[a]){
    if(e==b) continue;
    if(x==0){
      use[a]=1;
      ans.pb(a);
      x=3;
    }
    x=dfs(e,a,x-1);
    x--;
  }
  if(use[a]==0){
    use[a]=1;
    ans.pb(a);
    x=3;
  }
  return x;
}
bool dfs3(int a,int b,int idx){
  if(a==idx){
    ans.pb(a);
    return true;
  }
  for(auto e:g[a]){
    if(e==b) continue;
    if(dfs3(e,a,idx)){
      ans.pb(a);
      return true;
    }
  }
  return false;
}
void dfs2(int a,int b){
  for(auto e:g[a]){
    if(e==b) continue;
    p[e]+=p[a];
    dfs2(e,a);
  }
}
void Merge(deque<ll> &a,deque<ll> b){
  for(auto e:b) a.pb(e);
}
void solve(int i,int b){
  vector<int> v;
  for(auto e:g[i]){
    if(e==b) continue;
    solve(e,i);
    v.pb(e);
  }
  dp[i][0][1]=p[i];
  way[i][0][1]={i};
  dp[i][0][0]=p[i];
  ll idx=0,m=-inf;
  for(auto e:v) if(chmax(m,dp[e][0][0]-dp[e][0][1])) idx=e;
  dp[i][1][1]=p[i];
  for(auto e:v){
    if(e==idx) dp[i][1][1]+=dp[e][0][0];
    else dp[i][1][1]+=dp[e][0][1];
  }
  dp[i][0][0]=dp[i][1][1];
  for(auto e:v){
    if(e!=idx) Merge(way[i][1][1],way[e][0][1]);
  }
  for(auto e:v){
    if(e==idx) Merge(way[i][1][1],way[e][0][0]);
  }
  Merge(way[i][1][1],{i});
  Merge(way[i][0][0],{i});
  for(auto e:v){
    if(e==idx) Merge(way[i][0][0],way[e][1][1]);
  }
  for(auto e:v){
    if(e!=idx) Merge(way[i][0][0],way[e][0][1]);
  }
  ll sum=0;
  for(auto e:v) sum+=dp[e][0][1];
  dp[i][1][2]=p[i];
  way[i][1][2]={i};
  vector<P> A,B,C;
  if(v.size()){
  multiset<P> X,Y,Z;
  for(int j=0;j<v.size();j++){
    X.insert({dp[v[j]][0][0]-dp[v[j]][0][1],v[j]});
    Y.insert({dp[v[j]][1][1]-dp[v[j]][0][1],v[j]});
    Z.insert({dp[v[j]][0][2]-dp[v[j]][0][1],v[j]});
  }
  auto itr=X.end();
  itr--;
  for(int i=0;i<3;i++){
    A.pb((*itr));
    if(itr==X.begin()) break;
    itr--;
  }
  auto itr2=Y.end();
  itr2--;
  for(int i=0;i<3;i++){
    B.pb((*itr2));
    if(itr2==Y.begin()) break;
    itr2--;
  }
  auto itr3=Z.end();
  itr3--;
  for(int i=0;i<3;i++){
    C.pb((*itr3));
    if(itr3==Z.begin()) break;
    itr3--;
  }
  sor(A);
  sor(B);
  sor(C);
  rever(A);
  rever(B);
  rever(C);
  }
  for(int ii2=0;ii2<int(v.size())&&ii2<3;ii2++){
    for(int j2=0;j2<int(v.size())&&j2<3;j2++){
      for(int k2=0;k2<int(v.size())&&k2<3;k2++){
        int ii=A[ii2].se,j=B[j2].se,k=C[k2].se;
        if(ii==j||j==k||k==ii) continue;
        ll sum2=sum;
        sum2-=dp[ii][0][1]+dp[j][0][1]+dp[k][0][1];
        sum2+=dp[ii][0][0]+dp[j][1][1]+dp[k][0][2];
        sum2+=p[i];
        chmax(dp[i][1][2],sum2);
      }
    }
  }
  for(int ii=0;ii<int(v.size());ii++){
    for(int k=0;k<int(v.size());k++){
      if(k==ii) continue;
      ll sum2=sum;
      sum2-=dp[v[ii]][0][1]+dp[v[k]][0][1];
      sum2+=dp[v[ii]][0][0]+dp[v[k]][1][2];
      sum2+=p[i];
      chmax(dp[i][1][2],sum2);
    }
  }
  for(int k=0;k<int(v.size());k++){
    ll sum2=sum;
    sum2-=dp[v[k]][0][1];
    sum2+=dp[v[k]][1][2];
    sum2+=p[i];
    chmax(dp[i][1][2],sum2);
  }
  for(int ii2=0;ii2<int(v.size())&&ii2<3;ii2++){
    for(int j2=0;j2<int(v.size())&&j2<3;j2++){
      for(int k2=0;k2<int(v.size())&&k2<3;k2++){
        int ii=A[ii2].se,j=B[j2].se,k=C[k2].se;
        if(ii==j||j==k||k==ii) continue;
        ll sum2=sum;
        sum2-=dp[ii][0][1]+dp[j][0][1]+dp[k][0][1];
        sum2+=dp[ii][0][0]+dp[j][1][1]+dp[k][0][2];
        sum2+=p[i];
        if(dp[i][1][2]==sum2&&way[i][1][2].size()==1){
          way[i][1][2]={};
          for(int l=0;l<v.size();l++){
            if(v[l]!=ii&&v[l]!=j&&v[l]!=k) Merge(way[i][1][2],way[v[l]][0][1]);
          }
          Merge(way[i][1][2],way[ii][0][0]);
          Merge(way[i][1][2],{i});
          Merge(way[i][1][2],way[j][1][1]);
          Merge(way[i][1][2],way[k][0][2]);
        }
      }
    }
  }
  for(int ii=0;ii<int(v.size());ii++){
    for(int k=0;k<int(v.size());k++){
      if(k==ii) continue;
      ll sum2=sum;
      sum2-=dp[v[ii]][0][1]+dp[v[k]][0][1];
      sum2+=dp[v[ii]][0][0]+dp[v[k]][1][2];
      sum2+=p[i];
      if(dp[i][1][2]==sum2&&way[i][1][2].size()==1){
        if(i==3){
        //  cout<<"IIK"<<" "<<v[ii]<<" "<<v[k]<<endl;
        }
        way[i][1][2]={};
        for(int l=0;l<v.size();l++){
          if(l!=ii&&l!=k) Merge(way[i][1][2],way[v[l]][0][1]);
        }
        Merge(way[i][1][2],way[v[ii]][0][0]);
        Merge(way[i][1][2],{i});
        Merge(way[i][1][2],way[v[k]][1][2]);
      }
    }
  }
  for(int k=0;k<int(v.size());k++){
    ll sum2=sum;
    sum2-=dp[v[k]][0][1];
    sum2+=dp[v[k]][1][2];
    sum2+=p[i];
    if(dp[i][1][2]==sum2&&way[i][1][2].size()==1){
      way[i][1][2]={};
      for(int l=0;l<int(v.size());l++){
        if(l!=k) Merge(way[i][1][2],way[v[l]][0][1]);
      }
      Merge(way[i][1][2],{i});
      Merge(way[i][1][2],way[v[k]][1][2]);
    }
  }
  way[i][0][2]={i};
  dp[i][0][2]=p[i];
  for(int j=0;j<int(v.size());j++){
    for(int k=0;k<int(v.size());k++){
      if(j==k) continue;
      ll sum2=sum;
      sum2-=dp[v[j]][0][1]+dp[v[k]][0][1];
      sum2+=dp[v[j]][1][1]+dp[v[k]][0][2];
      sum2+=p[i];
      chmax(dp[i][0][2],sum2);
    }
  }
  for(int j=0;j<int(v.size());j++){
    for(int k=0;k<int(v.size());k++){
      if(j==k) continue;
      ll sum2=sum;
      sum2-=dp[v[j]][0][1]+dp[v[k]][0][1];
      sum2+=dp[v[j]][1][1]+dp[v[k]][0][2];
      sum2+=p[i];
      if(dp[i][0][2]==sum2&&way[i][0][2].size()==1){
        way[i][0][2]={i};
        Merge(way[i][0][2],way[v[j]][1][1]);
        for(int l=0;l<v.size();l++){
          if(l!=j&&l!=k) Merge(way[i][0][2],way[v[l]][0][1]);
        }
        Merge(way[i][0][2],way[v[k]][0][2]);
      }
    }
  }
  for(int j=0;j<v.size();j++){
    if(chmax(dp[i][0][2],dp[v[j]][1][2]+p[i])){
      way[i][0][2]={i};
      Merge(way[i][0][2],way[v[j]][1][2]);
    }
  }
 /* cout<<i<<endl;
  for(auto e:way[i][1][2]) cout<<e<<" ";
  cout<<endl;
  for(auto e:way[i][0][2]) cout<<e<<" ";
  cout<<endl;
  for(auto e:way[i][0][0]) cout<<e<<" ";
  cout<<endl;
  for(auto e:way[i][1][1]) cout<<e<<" ";
  cout<<endl; */
}
int main() {
  cincout();
  ll n,k;
  cin>>n>>k;
  if(k==3){
    for(int i=0;i<n-1;i++){
      ll a,b;
      cin>>a>>b;
      a--;
      b--;
      g[a].pb(b);
      g[b].pb(a);
    }
    ll sum=0;
    for(int i=0;i<n;i++){
      ll x;
      cin>>x;
      sum+=x;
    }
    dfs(0,-1,0);
    cout<<sum<<endl;
    cout<<n<<endl;
    for(int i=0;i<n;i++) cout<<ans[i]+1<<" ";
    cout<<endl;
    return 0;
  }
  if(k==1){
    for(int i=0;i<n-1;i++){
      ll a,b;
      cin>>a>>b;
      a--;
      b--;
      g[a].pb(b);
      g[b].pb(a);
    }
    for(int i=0;i<n;i++){
      cin>>p[i];
    }
    dfs2(0,-1);
    ll m=-inf,idx=0;
    for(int i=0;i<n;i++) if(chmax(m,p[i])) idx=i;
    cout<<p[idx]<<endl;
    dfs3(0,-1,idx);
    cout<<ans.size()<<endl;
    rever(ans);
    for(auto e:ans) cout<<e+1<<" ";
    cout<<endl;
    return 0;
  }
  if(k==2){
    for(int i=0;i<n-1;i++){
      ll a,b;
      cin>>a>>b;
      a--;
      b--;
      g[a].pb(b);
      g[b].pb(a);
    }
    for(int i=0;i<n;i++){
      cin>>p[i];
    }
    solve(0,-1);
    cout<<dp[0][0][2]<<endl;
    cout<<way[0][0][2].size()<<endl;
    for(auto e:way[0][0][2]) cout<<e+1<<" ";
    cout<<endl;
  }
}

詳細信息

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 2
Accepted
time: 103ms
memory: 818696kb

input:

2 1
1 2
255959470 961356354

output:

1217315824
2
1 2 

result:

ok correct!

Test #2:

score: -2
Wrong Answer
time: 112ms
memory: 818736kb

input:

1000 1
730 89
762 280
923 523
740 22
679 350
448 769
102 712
154 965
219 32
238 289
484 502
183 311
999 682
806 450
275 101
131 197
749 720
131 937
960 202
503 320
95 262
595 133
809 560
659 451
843 218
258 842
564 316
729 727
413 237
606 531
469 258
612 8
707 539
359 680
957 639
381 708
104 490
234...

output:

4395
1
1 

result:

wrong answer your profit is 4395 but jury's plan is 95535, your plan is not optimal!

Subtask #2:

score: 0
Wrong Answer

Test #12:

score: 7
Accepted
time: 153ms
memory: 818960kb

input:

2 2
2 1
243296356 635616793

output:

878913149
2
1 2 

result:

ok correct!

Test #13:

score: 0
Accepted
time: 120ms
memory: 818780kb

input:

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

output:

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

result:

ok correct!

Test #14:

score: -7
Wrong Answer
time: 184ms
memory: 819032kb

input:

200 2
150 170
21 33
96 152
143 26
136 70
92 159
34 164
163 182
74 115
93 61
151 83
81 119
10 146
114 170
39 83
139 4
173 41
193 96
87 57
14 164
107 51
45 15
157 17
43 183
96 30
11 137
55 18
138 81
87 12
112 122
159 82
195 185
75 71
16 191
33 88
70 195
149 114
106 160
96 118
92 44
9 141
107 143
151 2...

output:

972107044
1
1 

result:

wrong answer your profit is 972107044 but jury's plan is 57921623677, your plan is not optimal!

Subtask #3:

score: 0
Skipped

Dependency #2:

0%

Subtask #4:

score: 0
Skipped

Dependency #3:

0%

Subtask #5:

score: 0
Wrong Answer

Test #83:

score: 0
Wrong Answer
time: 139ms
memory: 816868kb

input:

2000 3
1359 90
1703 163
158 188
360 1501
195 664
1414 215
1546 1756
536 1096
1726 1223
1150 104
1757 703
1982 282
1023 998
1180 419
576 1759
1496 1993
44 670
1703 952
855 849
1998 1399
1280 980
1533 1090
1270 678
1680 387
469 1734
1799 263
473 588
303 226
5 295
1489 1471
1094 1667
1912 210
1368 1360...

output:

-705863364
2000
1 379 1954 1539 605 1300 1823 1866 1840 867 709 88 916 1526 1238 773 1919 816 1788 832 297 1349 1808 1428 183 863 951 1522 301 775 1265 936 1087 1672 565 1963 1330 926 1325 524 1334 36 46 1231 240 665 1376 289 1427 1319 1076 1880 1668 81 1504 1561 1987 1404 1630 1809 1354 295 1084 74...

result:

wrong answer your claimed profit is -705863364 but the profit of your plan is 1008611451196

Subtask #6:

score: 0
Skipped

Dependency #5:

0%