QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#669890#8760. 不等式HQLFWA 2ms4732kbC++202.7kb2024-10-23 19:58:162024-10-23 19:58:17

Judging History

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

  • [2024-10-23 19:58:17]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:4732kb
  • [2024-10-23 19:58:16]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
using ldb=long double;
using ull=unsigned long long;
const long long inf=0x3f3f3f3f3f3f3f3f;
using i128=__int128;
const int mod=1000000007;
const long double eps=0.00000000001;

struct DSU
{
    vector<int>f,siz;
    DSU() {}
    DSU(int n)
    {
        init(n);
    }

    void init(int n)
    {
        f.resize(n);
        std::iota(f.begin(), f.end(), 0);
        siz.assign(n, 1);
    }

    int find(int x)
    {
        while(x!=f[x])
        {
            x=f[x]=f[f[x]];
        }
        return x;
    }

    bool same(int x, int y)
    {
        return find(x)==find(y);
    }

    bool merge(int x,int y)
    {
        x=find(x);
        y=find(y);
        if(x==y)
        {
            return false;
        }
        siz[x]+=siz[y];
        f[y]=x;
        return true;
    }

    int size(int x)
    {
        return siz[find(x)];
    }
};

vector<pair<int,int>>a[200010];
i128 ans[200010];
DSU c{200010};
int f=0;

void dfs(int p)
{
    if(f==-1)return;
    if(a[p].empty())
    {
        ans[p]=1;
        return;
    }
    int la=(int)a[p].size();
    i128 mx=-inf;
    for(int i=0;i<=la-1;i++)
    {
        int x=a[p][i].first;
        int y=a[p][i].second;
        if(ans[x]==-1)dfs(x);
        if(ans[y]==-1)dfs(y);
        mx=max(mx,ans[x]+ans[y]);
    }
    if(mx>1000000000)f=-1;
    else ans[p]=mx;
}


void solve()
{
    int n,m;
    cin>>n>>m;
    for(int i=0;i<=n;i++)ans[i]=-1;
    for(int i=1;i<=m;i++)
    {
        int x,y,z;
        cin>>x>>y>>z;
        if(x==y||x==z)f=-1;
        int fax=c.find(x);
        if(fax==y)f=-1;
        if(fax==z)f=-1;
        if(f!=-1)
        {
            c.merge(x,y);
            c.merge(x,z);
            a[x].push_back({y,z});
        }
        else continue;
    }
    if(f==-1)
    {
        printf("-1\n");
        return;
    }
    vector<int> rt;
    for(int i=1;i<=n;i++)
    {
        if(c.f[i]==i)
        {
            rt.push_back(i);
        }
    }
    ll lr=(ll)rt.size();
    for(ll i=0;i<=lr-1;i++)
    {
        if(ans[rt[i]]==-1)dfs(rt[i]);
        if(f==-1)
        {
            printf("-1\n");
            return;
        }
    }
    i128 sum=0;
    for(ll i=1;i<=n;i++)
    {
        sum+=ans[i];
        if(sum>1000000000)
        {
            printf("-1\n");
            return;
        }
    }
    printf("%lld\n",(ll)sum);
}

int main()
{
    ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
    ll _=1;
//    cin>>_;
    while(_--)
    {
        solve();
    }
    return 0;
}



Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 4548kb

input:

3 1
1 2 2

output:

4

result:

ok 1 number(s): "4"

Test #2:

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

input:

3 1
1 2 3

output:

4

result:

ok 1 number(s): "4"

Test #3:

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

input:

3 1
1 2 2

output:

4

result:

ok 1 number(s): "4"

Test #4:

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

input:

3 1
1 2 2

output:

4

result:

ok 1 number(s): "4"

Test #5:

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

input:

3 1
1 2 2

output:

4

result:

ok 1 number(s): "4"

Test #6:

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

input:

5 1
1 2 2

output:

6

result:

ok 1 number(s): "6"

Test #7:

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

input:

5 1
1 2 2

output:

6

result:

ok 1 number(s): "6"

Test #8:

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

input:

5 1
1 2 2

output:

6

result:

ok 1 number(s): "6"

Test #9:

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

input:

5 1
1 2 3

output:

6

result:

ok 1 number(s): "6"

Test #10:

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

input:

5 2
1 2 3
2 3 4

output:

8

result:

ok 1 number(s): "8"

Test #11:

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

input:

10 1
1 2 3

output:

11

result:

ok 1 number(s): "11"

Test #12:

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

input:

10 1
1 2 2

output:

11

result:

ok 1 number(s): "11"

Test #13:

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

input:

10 2
1 2 3
2 3 4

output:

13

result:

ok 1 number(s): "13"

Test #14:

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

input:

10 2
1 2 2
2 3 4

output:

14

result:

ok 1 number(s): "14"

Test #15:

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

input:

10 3
1 2 3
1 8 8
2 3 3

output:

13

result:

ok 1 number(s): "13"

Test #16:

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

input:

20 1
1 2 2

output:

21

result:

ok 1 number(s): "21"

Test #17:

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

input:

20 2
1 2 3
2 3 3

output:

23

result:

ok 1 number(s): "23"

Test #18:

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

input:

20 3
7 14 6
1 2 3
4 7 20

output:

24

result:

ok 1 number(s): "24"

Test #19:

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

input:

20 4
1 2 2
6 10 6
2 3 3
3 4 5

output:

-1

result:

ok 1 number(s): "-1"

Test #20:

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

input:

20 5
1 17 3
1 2 3
2 3 4
3 4 5
8 13 16

output:

28

result:

ok 1 number(s): "28"

Test #21:

score: -100
Wrong Answer
time: 1ms
memory: 4548kb

input:

200 9
1 2 2
2 3 3
3 4 5
91 112 195
126 145 82
4 5 5
53 2 15
5 6 6
6 7 7

output:

269

result:

wrong answer 1st numbers differ - expected: '318', found: '269'