QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#235685 | #1957. Friendship Graphs | _Abtahi_# | WA | 43ms | 7464kb | C++17 | 2.2kb | 2023-11-03 00:53:54 | 2023-11-03 00:53:55 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int,int> pii;
typedef vector<int> vii;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef unordered_map<int,int> umap;
typedef long double ld;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define popcount __builtin_popcount
#define case cout<<"Case "<<__testcase-testcase<<":";
#define endl '\n'
vector<vii> g;
vii v;
int w,b;
bool dfs(int u,int p,int c){
if(v[u]>-1 && v[u]==c) return false;
else if(v[u]==1-c) return true;
v[u]=c;
if(c==0) w++;
else b++;
for(auto i: g[u]) if(i!=p) if(dfs(i,u,1-c)) return true;
return false;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int testcase=1;
//cin>>testcase;
int __testcase=testcase;
while(testcase--){
int n,m,i,j,k;
cin>>n>>m;
int e[n+5][n+5];
for(i=0;i<n+5;i++) for(j=0;j<n+5;j++) e[i][j]=0;
while(m--){
int u,v;
cin>>u>>v;
e[u][v]=1;
e[v][u]=1;
}
g.resize(n+5);
v.assign(n+5,-1);
for(i=1;i<=n;i++) for(j=1;j<=n;j++) if(e[i][j]==0 && i!=j) g[i].pb(j);
vii p;
int one=0;
for(i=1;i<=n;i++){
if(v[i]>=0) continue;
w=b=0;
if(dfs(i,-1,0)) break;
if(w!=b) p.pb(abs(w-b));
}
if(i<=n){
cout<<-1<<endl;
continue;
}
int total=0; for(auto i: p) total+=i;
n=p.size();
m=total/2;
int dp[2][m+5];
for(i=0;i<=m;i++) dp[1][i]=0;
dp[1][0]=1;
int idx, pre;
for(i=0;i<n;i++){
idx=i%2;
pre=1-idx;
for(j=0;j<=m;j++) dp[idx][j]=0;
for(j=0;j<=m;j++){
if(j+p[i]<=m) dp[idx][j+p[i]]|=dp[pre][j];
dp[idx][j]|=dp[pre][j];
}
}
//for(auto i: p) cout<<i<<" "; cout<<endl;
int ans=n;
for(i=0;i<=m;i++) if(dp[idx][i]) ans=min(ans,total-2*i);
cout<<ans<<endl;
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 43ms
memory: 7400kb
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: 42ms
memory: 7464kb
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:
1
result:
wrong answer 1st lines differ - expected: '2', found: '1'