QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#508904#8760. 不等式herry1234567WA 0ms3860kbC++201.3kb2024-08-07 21:42:212024-08-07 21:42:23

Judging History

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

  • [2024-08-07 21:42:23]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3860kb
  • [2024-08-07 21:42:21]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e4+10,M=2e4+10;
int to[N],h[N],e[M],ne[M],idx;
ll n, m,cnt;
ll dist[N];
ll w[N];
void add(ll a,ll b)
{
    e[idx] = b;
    ne[idx] = h[a];
    h[a] = idx++;
}

bool topsort()
{
     cnt = 0;
    queue<int>q;
    for (int i = 1; i <= n; i++)
    {
        if (!to[i])
        {
            q.push(i);
            cnt++;
            w[i] = 1;
        }     
    }
    while (!q.empty())
    {
        auto u = q.front();
        q.pop();
        for (int i = h[u]; ~i; i=ne[i])
        {
            ll j = e[i];
            w[j] += w[u];
            if (--to[j] == 0)
            {
                q.push(j);
                cnt++;
            }
        }
    }

    return cnt == n ;
}


int main()
{
    ios::sync_with_stdio(0), cin.tie(0);
    memset(h, -1, sizeof(h));
    cin >> n >> m;
    ll a, b, c;
    for (int i = 0; i < m; i++)
    {
        cin >>a>> b>> c;
        add(b, a);
        add(c, a);
        to[a]++;
        to[a]++;
    }
    
    if (!topsort())cout << "-1" << endl;
    else
    {
        ll sum = 0;
        for (int i =1; i <=cnt; i++)
        {
           // cout << w[i] << " ";
            sum += w[i];
        }
       // cout << endl;
        cout << sum << endl;
    }

}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

3 1
1 2 2

output:

4

result:

ok 1 number(s): "4"

Test #2:

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

input:

3 1
1 2 3

output:

4

result:

ok 1 number(s): "4"

Test #3:

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

input:

3 1
1 2 2

output:

4

result:

ok 1 number(s): "4"

Test #4:

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

input:

3 1
1 2 2

output:

4

result:

ok 1 number(s): "4"

Test #5:

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

input:

3 1
1 2 2

output:

4

result:

ok 1 number(s): "4"

Test #6:

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

input:

5 1
1 2 2

output:

6

result:

ok 1 number(s): "6"

Test #7:

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

input:

5 1
1 2 2

output:

6

result:

ok 1 number(s): "6"

Test #8:

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

input:

5 1
1 2 2

output:

6

result:

ok 1 number(s): "6"

Test #9:

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

input:

5 1
1 2 3

output:

6

result:

ok 1 number(s): "6"

Test #10:

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

input:

5 2
1 2 3
2 3 4

output:

8

result:

ok 1 number(s): "8"

Test #11:

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

input:

10 1
1 2 3

output:

11

result:

ok 1 number(s): "11"

Test #12:

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

input:

10 1
1 2 2

output:

11

result:

ok 1 number(s): "11"

Test #13:

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

input:

10 2
1 2 3
2 3 4

output:

13

result:

ok 1 number(s): "13"

Test #14:

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

input:

10 2
1 2 2
2 3 4

output:

14

result:

ok 1 number(s): "14"

Test #15:

score: -100
Wrong Answer
time: 0ms
memory: 3572kb

input:

10 3
1 2 3
1 8 8
2 3 3

output:

15

result:

wrong answer 1st numbers differ - expected: '13', found: '15'