QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#46705#1957. Friendship GraphsmeomeoWA 248ms4912kbC++1.9kb2022-08-31 00:24:132022-08-31 00:24:14

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-08-31 00:24:14]
  • 评测
  • 测评结果:WA
  • 用时:248ms
  • 内存:4912kb
  • [2022-08-31 00:24:13]
  • 提交

answer

#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define int long long
#define fi first
#define se second

using namespace std;
using namespace __gnu_pbds;

template <class T> using Tree = tree<T, null_type, less<T>,
rb_tree_tag,tree_order_statistics_node_update>;

const int maxn = 1001;

bool color[maxn], c[maxn][maxn], vis[maxn], ok = 0;
int n;
int res;

void dfs(int u) {
    vis[u] = 1;

    if (color[u] == 0) {
        res++;
    }
    else {
        res--;
    }

    for (int i = 0 ; i < n ; i++) {
        if (!c[u][i]) continue;
        if (u == i) continue;
        if (!vis[i]) {
            color[i] = color[u] ^ 1;
            dfs(i);
        }
        else {
            if (color[i] == color[u]) {
                ok = 1;
            }
        }
    }
}

signed main(){
    ios::sync_with_stdio(0); cin.tie(0);
//    freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);

    int m;

    cin >> n >> m;

    for (int i = 0 ; i < n ; i++) {
        for (int j = 0 ; j < n ; j++) {
            c[i][j] = 1;
        }
    }

    for (int i = 0 ; i < m ; i++) {
        int u, v;

        cin >> u >> v;

        c[u-1][v-1] = c[v-1][u-1] = 0;
    }

    vector<pair<int, int>> bag;

    for (int i = 0 ; i < n ; i++) {
        if (!vis[i]) {
            dfs(i);
            bag.push_back({res, -res});
            res = 0;
        }
    }

    map<int, int> dp, pre;

    pre[0] = 1;

    for (auto &[u, v]: bag) {
        for (int i = -1000 ; i <= 1000 ; i++) {
            dp[i] |= pre[i - u];
            dp[i] |= pre[i - v];
        }
    }

    res = 1E18;

    for (int i = -1000 ; i <= 1000 ; i++) {
        if (dp[i]) {
            res = min(res, abs(i));
        }
    }

    if (!ok) {
        cout << abs(res);
    }
    else {
        cout << -1;
    }
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 245ms
memory: 4912kb

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: -100
Wrong Answer
time: 248ms
memory: 4912kb

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:

0

result:

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