Submission #2736602


Source Code Expand

Copy
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }
//---------------------------------------------------------------------------------------------------
/*---------------------------------------------------------------------------------------------------
            ∧_∧  
      ∧_∧  (´<_` )  Welcome to My Coding Space!
     ( ´_ゝ`) /  ⌒i     
    /   \     | |     
    /   / ̄ ̄ ̄ ̄/  |  
  __(__ニつ/     _/ .| .|____  
     \/____/ (u ⊃  
---------------------------------------------------------------------------------------------------*/
 
 
 
 
 
int N, M;
int _E[707][707];
vector<int> E[707];
//---------------------------------------------------------------------------------------------------
int vis[707], col[707];
pair<int, int> dfs(int cu, int co = 0) {
    vis[cu] = 1;
    col[cu] = co;
 
    pair<int, int> res = { 0, 0 };
 
    if (co == 0) res.first++;
    else res.second++;
 
    fore(to, E[cu]) {
        if (vis[to]) {
            if (col[to] == co) return { -1, -1 };
        } else {
            auto p = dfs(to, 1 - co);
            if (p.first < 0) return { -1,-1 };
            res.first += p.first;
            res.second += p.second;
        }
    }
 
    return res;
}
//---------------------------------------------------------------------------------------------------
int dp[707][1007];
void _main() {
    cin >> N >> M;
    rep(i, 0, M) {
        int a, b; cin >> a >> b;
        a--; b--;
        _E[a][b] = _E[b][a] = 1;
    }
    rep(i, 0, N) rep(j, 0, N) if (!_E[i][j] and i != j) E[i].push_back(j);
 
    vector<pair<int, int>> v;
    rep(i, 0, N) if (!vis[i]) {
        auto p = dfs(i);
        if (p.first < 0) {
            printf("-1\n");
            return;
        }
        v.push_back(p);
    }
 
    int n = v.size();
    dp[0][0] = 1;
    rep(i, 0, n) rep(cn, 0, 707) if(dp[i][cn]) {
        dp[i+1][cn + v[i].first] = 1;
        dp[i+1][cn + v[i].second] = 1;
    }
 
    int ans = inf;
    rep(cn, 0, 707) if(dp[n][cn]) {
        int taka = cn;
        int hashi = N - cn;
        chmin(ans, taka * (taka - 1) / 2 + hashi * (hashi - 1) / 2);
    }
    cout << ans << endl;
}

Submission Info

Submission Time
Task E - Independence
User hamayanhamayan
Language C++14 (GCC 5.4.1)
Score 700
Code Size 2890 Byte
Status AC
Exec Time 37 ms
Memory 5120 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 700 / 700
Status
AC × 4
AC × 37
Set Name Test Cases
Sample sample1.txt, sample2.txt, sample3.txt, sample4.txt
All sample1.txt, sample2.txt, sample3.txt, sample4.txt, 1.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 2.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 3.txt, 4.txt, 5.txt, 6.txt, 7.txt, 8.txt, 9.txt, sample1.txt, sample2.txt, sample3.txt, sample4.txt
Case Name Status Exec Time Memory
1.txt AC 2 ms 2304 KB
10.txt AC 2 ms 2304 KB
11.txt AC 35 ms 3200 KB
12.txt AC 34 ms 3200 KB
13.txt AC 33 ms 3328 KB
14.txt AC 29 ms 3584 KB
15.txt AC 36 ms 3072 KB
16.txt AC 33 ms 2944 KB
17.txt AC 35 ms 3584 KB
18.txt AC 8 ms 2432 KB
19.txt AC 37 ms 4224 KB
2.txt AC 8 ms 2432 KB
20.txt AC 36 ms 3328 KB
21.txt AC 34 ms 4224 KB
22.txt AC 35 ms 3712 KB
23.txt AC 37 ms 4992 KB
24.txt AC 23 ms 4608 KB
25.txt AC 6 ms 5120 KB
26.txt AC 2 ms 2304 KB
27.txt AC 2 ms 2304 KB
28.txt AC 2 ms 2304 KB
29.txt AC 2 ms 2304 KB
3.txt AC 36 ms 3072 KB
4.txt AC 35 ms 3072 KB
5.txt AC 33 ms 2944 KB
6.txt AC 33 ms 3200 KB
7.txt AC 36 ms 4096 KB
8.txt AC 2 ms 2688 KB
9.txt AC 8 ms 2432 KB
sample1.txt AC 2 ms 2304 KB
sample2.txt AC 1 ms 2304 KB
sample3.txt AC 2 ms 2304 KB
sample4.txt AC 2 ms 2304 KB