QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#688794#1957. Friendship Graphsk1nsom#WA 136ms11664kbC++171.9kb2024-10-30 13:35:452024-10-30 13:35:47

Judging History

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

  • [2024-10-30 13:35:47]
  • 评测
  • 测评结果:WA
  • 用时:136ms
  • 内存:11664kb
  • [2024-10-30 13:35:45]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;
#define int long long
#define endl '\n'
#define PII pair<int, int>
const int N = 1e3 + 10;
int n, m, dp[2][N], vis[N];
vector<int> e[N];
vector<int> tmp, g[N];

void dfs(int u)
{
    tmp.push_back(u);
    for (auto to : g[u])
        if (!vis[to])
            vis[to] = 3 ^ vis[u], dfs(to);
        else if (vis[to] == vis[u])
        {
            cout << -1 << endl;
            exit(0);
        }
}

int top = 0;
PII stk[N];

void solve()
{
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        e[i].resize(n + 1), fill(e[i].begin(), e[i].end(), 1);
    for (int i = 1; i <= n; i++)
        e[i][i] = 0;
    for (int i = 1; i <= m; i++)
    {
        int u, v;
        cin >> u >> v;
        e[u][v] = 0, e[v][u] = 0;
    }
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            if (e[i][j])
                g[i].push_back(j);
        }
    }
    for (int i = 1; i <= n; i++)
        if (!vis[i])
        {
            vis[i] = 1;
            tmp.clear();
            dfs(i);
            int cnt = 0;
            for (auto to : tmp)
                if (vis[to] == 1)
                    cnt++;
            stk[++top] = {cnt, tmp.size() - cnt};
        }
    dp[0][0] = 1;
    for (int i = 1; i <= top; i++)
    {
        for (int j = n; j >= stk[i].first; j--)
            dp[i & 1][j] |= dp[1 ^ (i & 1)][j - stk[i].first];
        for (int j = n; j >= stk[i].second; j--)
            dp[i & 1][j] |= dp[1 ^ (i & 1)][j - stk[i].second];
    }
    int ans = n;
    for (int i = 1; i <= n; i++)
        if (dp[top & 1][i])
            ans = min(ans, abs(n - i - i));
    cout << ans << endl;
}

signed main()
{
    // ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int t = 1;
    // cin >> t;
    while (t--)
        solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 128ms
memory: 11508kb

input:

1000 499000
20 615
260 390
779 37
13 563
650 605
358 684
783 370
162 90
379 662
88 208
458 371
178 3
590 762
455 514
738 641
270 805
205 881
205 315
837 54
976 579
519 532
982 669
563 804
648 274
268 293
182 392
337 772
961 603
294 607
546 772
189 218
702 266
515 610
691 965
643 235
509 193
184 302
...

output:

0

result:

ok single line: '0'

Test #2:

score: 0
Accepted
time: 132ms
memory: 11572kb

input:

1000 498999
35 65
880 835
501 309
590 758
882 857
356 493
43 623
644 637
361 785
58 317
26 11
595 521
723 629
611 36
789 29
30 650
426 475
852 862
667 137
677 173
656 44
457 279
680 567
789 989
368 873
510 721
128 584
835 956
419 779
607 568
317 790
932 580
336 400
74 265
772 855
939 816
448 883
381...

output:

2

result:

ok single line: '2'

Test #3:

score: 0
Accepted
time: 131ms
memory: 11632kb

input:

1000 498998
667 721
880 868
426 900
882 839
789 440
590 395
356 847
852 758
35 648
26 723
611 329
644 560
723 45
595 813
501 338
58 762
30 302
43 340
361 734
74 863
128 433
656 196
677 188
932 651
835 603
368 568
336 105
317 225
457 350
419 771
607 545
789 31
772 465
510 542
680 888
504 445
884 999
...

output:

2

result:

ok single line: '2'

Test #4:

score: 0
Accepted
time: 127ms
memory: 11540kb

input:

1000 499499
552 449
897 36
561 770
188 194
233 385
689 608
814 604
386 789
440 778
51 295
368 726
835 647
182 31
387 250
202 887
607 184
189 192
54 774
252 403
562 109
878 528
258 449
823 460
619 906
952 96
69 383
630 81
474 996
273 651
749 270
682 976
147 209
287 612
402 108
575 479
864 462
1000 72...

output:

0

result:

ok single line: '0'

Test #5:

score: -100
Wrong Answer
time: 136ms
memory: 11664kb

input:

1000 498751
681 409
733 39
732 717
449 73
41 314
80 971
61 44
139 989
93 338
235 188
113 955
362 380
471 183
228 566
120 625
543 1
450 262
411 197
470 342
461 616
704 186
345 782
499 672
391 288
541 531
917 742
736 819
345 428
821 265
321 789
270 742
452 609
386 668
60 440
421 23
386 677
167 69
944 ...

output:

2

result:

wrong answer 1st lines differ - expected: '498', found: '2'