QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#65982#1957. Friendship Graphsdlg#WA 87ms5252kbC++172.8kb2022-12-05 10:08:232022-12-05 10:08:26

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-12-05 10:08:26]
  • 评测
  • 测评结果:WA
  • 用时:87ms
  • 内存:5252kb
  • [2022-12-05 10:08:23]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define sp ' '
#define endl '\n'
#define loop(i, l, r) for(int i=(int)(l); i<=(int)(r); i++)
#define all(x) (x).begin(), (x).end()

const int N = 1005;

struct SolverGraph{
    int n,m;
    bool mark[N][N];
    int color[N];
    vector<vector<int> > a;

    SolverGraph(){
        memset(mark,0,sizeof mark);
        memset(color,-1,sizeof color);
        a.assign(N,vector<int>());
    }

    void genComp(){
        loop(i,1,n){
            loop(j,1,n){
                if (i==j||mark[i][j]==true) continue;
                a[i].push_back(j);
            }
        }
    }

    int colorComp(int root){
        int ret = 0;
        queue<int> q; q.push(root); color[root] = 0;
        while(!q.empty()){
            auto u = q.front(); q.pop();
            if (color[u]==0) ret++;
            for(auto v:a[u]){
                if (color[v]==-1) {
                    color[v] = color[u] ^ 1;
                    q.push(v);
                }
                else{
                    if (color[v]!=(color[u]^1)){
                        return -1;
                    }
                }
            }
        }
        return ret;
    }

    pair<int,vector<int>> operator()(){
        cin >> n >> m;
        loop(i,1,m){
            int u,v; cin >> u >> v;
            mark[u][v] = mark[v][u] = true;
        }
        genComp();

        vector<int> ret;
        loop(i,1,n){
            if (color[i]==-1){
                auto rec = colorComp(i);
                if (rec==-1){
                    return {-1,{}};
                }
                ret.push_back(rec);
            }
        }
        return {n,ret};
    }
};

struct SolverKnapsack{
    int total;
    vector<int> a;
    bool dp[N][N];
    SolverKnapsack(int _total, vector<int> _a):total(_total),a(_a){
        memset(dp,0,sizeof dp);
    };

    int operator()(){
        // return the smallest diff
        dp[0][0] = true;
        loop(i,1,a.size()){
            int item = a[i-1];
            loop(j,item,total){
                if (dp[i-1][j-item]){
                    dp[i][j] = true;
                    break;
                }
            }
        }

        int ans = total;
        loop(i,0,total){
            if (dp[a.size()][i]==false) continue;
            ans = min(ans,abs(i-(total-i)));
        }
        return ans;
    }
};

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
#ifdef LOCAL
    freopen("1test.inp","r",stdin);
#endif
    SolverGraph solve1;
    auto [n,whites] = solve1();
    if (n==-1){
        cout << -1 << endl;
        return 0;
    }

    SolverKnapsack solve2(n,whites);
    cout << solve2() << endl;
}

详细

Test #1:

score: 0
Wrong Answer
time: 87ms
memory: 5252kb

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:

2

result:

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