QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#612285#9349. Exchanging GiftsYour-SunWA 201ms3776kbC++201.6kb2024-10-05 10:13:522024-10-05 10:13:53

Judging History

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

  • [2024-10-05 10:13:53]
  • 评测
  • 测评结果:WA
  • 用时:201ms
  • 内存:3776kb
  • [2024-10-05 10:13:52]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define endl '\n'

void solve()
{
    int n;
    cin>>n;
    vector<vector<ll>> arr(n+1);
    vector<pii> b(n+1);
    ll i,j,k;
    for(i=1;i<=n;i++)
    {
        int op;
        cin>>op;
        if(op==1)
        {
            int m;
            cin>>m;
            arr[i].resize(m);
            for(j=0;j<m;j++)
                cin>>arr[i][j];
        }
        else    // op==2
            cin>>b[i].first>>b[i].second;
    }

    if(!b[n].first)
    {
        ll mx=0;
        map<ll,ll> mp;
        for(auto x:arr[n])
            mx=max(mx,++mp[x]);
        if(2LL*mx<=arr[n].size())
            cout<<arr[n].size()<<endl;
        else
            cout<<2LL*(arr[n].size()-mx)<<endl;
        return;
    }

    vector<ll> dp(n+1);
    dp[n]=1;
    auto fun=[&](auto fun, int u)
    {
        auto [x,y]=b[u];
        if(!x)
            return;
        dp[x]+=dp[u], dp[y]+=dp[u];
        fun(fun,x), fun(fun,y);
    };
    fun(fun,n);
    ll mx=0, cnt=0;
    map<ll,ll> mp;
    for(i=1;i<=n;i++)
    {
        if(!dp[i] || arr[i].empty())
            continue;
        for(auto x:arr[i])
        {
            mp[x]+=dp[i];
            cnt+=dp[i];
            mx=max(mx,mp[x]);
        }
    }
    if(2LL*mx<=cnt)
        cout<<cnt<<endl;
    else
        cout<<2LL*(cnt-mx)<<endl;
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    int T;
    cin>>T;
    while(T--)
        solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

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

output:

4
6

result:

ok 2 lines

Test #2:

score: -100
Wrong Answer
time: 201ms
memory: 3776kb

input:

10000
100
1 30 371028678 371028678 371028678 716418076 398221499 591504380 398221499 398221499 591504380 777141390 398221499 591504380 591504380 777141390 287847807 716418076 777141390 716418076 777141390 287847807 287847807 287847807 371028678 371028678 398221499 777141390 371028678 6827702 6827702...

output:

9102
980
5224
1170
180
17836
194
1338
6616
0
2512
300
35
1438
124164
307
12911
706
1511
1691
2273681
2138
9468
199
5664
686
0
81
61143
1005
509
8881
138
147331
91
1146683
310500
21618
2324
8150
2307
1506
1943
87858
0
971
11020
63
0
685
3154
2228
41
18
91
1757
2526
112509
554
6187
45509
119876
176
23...

result:

wrong answer 1st lines differ - expected: '700', found: '9102'