Submission #74230513
Source Code Expand
#include <bits/stdc++.h>
#include<math.h>
using namespace std;
typedef long double ld;
typedef long long ll;
typedef vector<long long> vll;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
//#define endl '\n'
ll mod = 1e9+7 ;
int p1 = 31 , p2 = 91 ;
ll mod1= 1e9+7 , mod2 = 1e9+9 ;
void dfs(int node , int parent , vector<vector<int>>& adj , vector<int>&visited, int cur ,vector<int>&depth) {
visited[node] = cur ;
depth[node] = depth[parent] + 1;
for(auto it : adj[node]) {
if(it == parent) continue ;
if(!visited[it]) dfs(it, node, adj , visited, cur, depth) ;
}
}
bool dfs1(int node , int parent , vector<int>&colored , vector<vector<int>> &adj) {
if(node == parent) colored[node] = 0 ;
else colored[node] = colored[parent]^1 ;
bool test = 1 ;
for(auto it : adj[node]) {
if(it == parent) continue ;
if(colored[it ] == -1) test&=dfs1(it, node , colored , adj);
else {
if(colored[it] == colored[node]) return 0 ;
}
}
return test ;
}
void solve() {
int n , m ; cin >> n >> m ;
vector<vector<int>> adj(n) ;
for(int i = 0 ; i < m ; ++i) {
int u , v ; cin>> u >> v ;
u-- ;v-- ;
adj[u].push_back(v);
adj[v].push_back(u) ;
}
vector<int> visited(n,0) ;
vector<vector<int>> adj2 = adj ;
vector<int> colored (n,-1) ;
bool test = 1 ;
for(int i = 0 ; i < n ;++i) {
if(colored[i] == -1) test&=dfs1(i,i,colored ,adj2 );
}
if(!test) {
cout << 0 << endl ;
}else{
vector<int> depth(n,-1) ;
int cur = 1 ;
ll ans = 0 ;
for(int i = 0 ; i < n ; ++i) {
if(!visited[i]) {
dfs(i,i , adj , visited , cur , depth);
cur ++;
}
}
// for(auto it : visited) cout << it << ' ' ;
// cout << endl ;
// for(auto it : depth) cout << it << ' ' ;
// cout << endl ;
map<int,pair<ll,ll>> mp ;
for(int i = 0 ; i < n ;++i) {
if(depth[i]%2) mp[visited[i]].first++;
else mp[visited[i]].second++;
}
ans -= m ;
ll ans1 = 0 ;
for(auto it : mp) {
ans += it.second.second * it.second.first ;
ans1+= 1ll* (it.second.second + it.second.first) * (n - (it.second.second + it.second.first) ) ;
}
cout << ans +ans1/2 << endl ;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
//cin >> t;
while (t--) {
solve();
}
return 0;
}
Submission Info
| Submission Time | |
|---|---|
| Task | G - Make Bipartite 2 |
| User | Code_Pro |
| Language | C++23 (GCC 15.2.0) |
| Score | 400 |
| Code Size | 2723 Byte |
| Status | AC |
| Exec Time | 115 ms |
| Memory | 46212 KiB |
Judge Result
| Set Name | Sample | All | ||||
|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 400 / 400 | ||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | example0.txt, example1.txt, example2.txt |
| All | 000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt, 022.txt, 023.txt, 024.txt, 025.txt, 026.txt, 027.txt, 028.txt, 029.txt, 030.txt, 031.txt, 032.txt, 033.txt, 034.txt, 035.txt, 036.txt, 037.txt, 038.txt, 039.txt, 040.txt, 041.txt, 042.txt, 043.txt, example0.txt, example1.txt, example2.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| 000.txt | AC | 1 ms | 3628 KiB |
| 001.txt | AC | 1 ms | 3452 KiB |
| 002.txt | AC | 44 ms | 27620 KiB |
| 003.txt | AC | 6 ms | 14260 KiB |
| 004.txt | AC | 48 ms | 29052 KiB |
| 005.txt | AC | 115 ms | 39860 KiB |
| 006.txt | AC | 111 ms | 46212 KiB |
| 007.txt | AC | 94 ms | 45528 KiB |
| 008.txt | AC | 13 ms | 7772 KiB |
| 009.txt | AC | 13 ms | 7092 KiB |
| 010.txt | AC | 13 ms | 6876 KiB |
| 011.txt | AC | 17 ms | 10136 KiB |
| 012.txt | AC | 62 ms | 26116 KiB |
| 013.txt | AC | 25 ms | 10716 KiB |
| 014.txt | AC | 69 ms | 28116 KiB |
| 015.txt | AC | 50 ms | 28328 KiB |
| 016.txt | AC | 41 ms | 27608 KiB |
| 017.txt | AC | 40 ms | 27484 KiB |
| 018.txt | AC | 42 ms | 27572 KiB |
| 019.txt | AC | 71 ms | 25556 KiB |
| 020.txt | AC | 5 ms | 4708 KiB |
| 021.txt | AC | 18 ms | 8416 KiB |
| 022.txt | AC | 6 ms | 4960 KiB |
| 023.txt | AC | 6 ms | 4948 KiB |
| 024.txt | AC | 70 ms | 29408 KiB |
| 025.txt | AC | 44 ms | 27876 KiB |
| 026.txt | AC | 39 ms | 27484 KiB |
| 027.txt | AC | 39 ms | 27612 KiB |
| 028.txt | AC | 39 ms | 27596 KiB |
| 029.txt | AC | 34 ms | 11596 KiB |
| 030.txt | AC | 27 ms | 9816 KiB |
| 031.txt | AC | 19 ms | 8536 KiB |
| 032.txt | AC | 16 ms | 7708 KiB |
| 033.txt | AC | 2 ms | 4004 KiB |
| 034.txt | AC | 81 ms | 29532 KiB |
| 035.txt | AC | 43 ms | 27560 KiB |
| 036.txt | AC | 48 ms | 28252 KiB |
| 037.txt | AC | 43 ms | 27672 KiB |
| 038.txt | AC | 41 ms | 27592 KiB |
| 039.txt | AC | 94 ms | 30284 KiB |
| 040.txt | AC | 43 ms | 14872 KiB |
| 041.txt | AC | 16 ms | 8232 KiB |
| 042.txt | AC | 15 ms | 7504 KiB |
| 043.txt | AC | 1 ms | 3696 KiB |
| example0.txt | AC | 1 ms | 3608 KiB |
| example1.txt | AC | 1 ms | 3608 KiB |
| example2.txt | AC | 1 ms | 3600 KiB |