Submission #3635448
Source Code Expand
#include "bits/stdc++.h" using namespace std; using ll = long long; using ull = unsigned long long; typedef pair< int, int > Pi; typedef pair< long long, long long > P; typedef pair< long long, P > PP; typedef pair< P, P > PPP; const double DINF = 5e14, eps = 1e-10; const long long MOD = 1e9 + 7, INF = 5e18; const int di[4] = { 1,0,-1,0 }, dj[4] = { 0,1,0,-1 }; #define fr first #define sc second #define pb push_back #define eb emplace_back #define ALL(x) (x).begin(),(x).end() int n,m,a,b; bool r[710][710]; int cl[710],vi[710]; bool dp[710][710]; vector<int>cg[710]; vector<P>v; P dfs(int now,int color){ vi[now]=1; cl[now]=color; P ret=P(0,0); if(color==1)ret.fr++; else ret.sc++; for(int u:cg[now]){ if(vi[u]&&cl[u]==cl[now])return P(-1,-1); else if(!vi[u]){ int c; if(color==1)c=2; else c=1; P p=dfs(u,c); if(p.fr<0)return P(-1,-1); ret.fr+=p.fr; ret.sc+=p.sc; } } return ret; } void set_up(){ cin>>n>>m; for(int i=0;i<m;i++){ cin>>a>>b; a--,b--; r[a][b]=true; r[b][a]=true; } for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ if(!r[i][j]&&i!=j)cg[i].eb(j); } } } void solve(){ for(int i=0;i<n;i++){ if(vi[i])continue; P p=dfs(i,1); if(p.fr==-1){ cout<<-1<<endl; return; } v.eb(p); } int ans=1e9; dp[0][0]=true; for(int i=0;i<v.size();i++){ for(int j=0;j<n;j++){ if(dp[i][j]){ dp[i+1][j+v[i].fr]=true; dp[i+1][j+v[i].sc]=true; } } } for(int i=0;i<n;i++){ if(dp[v.size()][i])ans=min(ans,i*(i-1)/2+(n-i)*(n-i-1)/2); } cout<<ans<<endl; } int main() { set_up(); solve(); return 0; }
Submission Info
Submission Time | |
---|---|
Task | E - Independence |
User | kakakakaneko |
Language | C++14 (GCC 5.4.1) |
Score | 700 |
Code Size | 1690 Byte |
Status | AC |
Exec Time | 127 ms |
Memory | 3072 KB |
Judge Result
Set Name | Sample | All | ||||
---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 700 / 700 | ||||
Status |
|
|
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 | 3 ms | 384 KB |
10.txt | AC | 3 ms | 384 KB |
11.txt | AC | 120 ms | 896 KB |
12.txt | AC | 116 ms | 1024 KB |
13.txt | AC | 106 ms | 1024 KB |
14.txt | AC | 96 ms | 1408 KB |
15.txt | AC | 124 ms | 768 KB |
16.txt | AC | 118 ms | 768 KB |
17.txt | AC | 120 ms | 896 KB |
18.txt | AC | 24 ms | 512 KB |
19.txt | AC | 127 ms | 1024 KB |
2.txt | AC | 22 ms | 512 KB |
20.txt | AC | 126 ms | 896 KB |
21.txt | AC | 117 ms | 1024 KB |
22.txt | AC | 121 ms | 896 KB |
23.txt | AC | 127 ms | 1280 KB |
24.txt | AC | 77 ms | 1024 KB |
25.txt | AC | 5 ms | 3072 KB |
26.txt | AC | 1 ms | 256 KB |
27.txt | AC | 1 ms | 256 KB |
28.txt | AC | 1 ms | 256 KB |
29.txt | AC | 1 ms | 256 KB |
3.txt | AC | 125 ms | 768 KB |
4.txt | AC | 121 ms | 896 KB |
5.txt | AC | 116 ms | 896 KB |
6.txt | AC | 111 ms | 1024 KB |
7.txt | AC | 126 ms | 1024 KB |
8.txt | AC | 4 ms | 384 KB |
9.txt | AC | 23 ms | 512 KB |
sample1.txt | AC | 1 ms | 256 KB |
sample2.txt | AC | 1 ms | 256 KB |
sample3.txt | AC | 1 ms | 256 KB |
sample4.txt | AC | 1 ms | 256 KB |