Submission #69089518


Source Code Expand

/*
Author : Mikira
9/6/2025 10:00:24 PM
*/

#include "bits/stdc++.h"
using namespace std;

typedef long long LL;
typedef vector<int> vi;
typedef vector<LL> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> PII;
typedef pair<int, int> pii;
typedef pair<LL, LL> PLL;
typedef pair<LL, LL> pll;
typedef long double ld;

#define rep(i, a, b) for(int i = a; i < int(b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define popf pop_front
#define pf push_front
#define popb pop_back
#define pb push_back
#define fi first
#define se second

const double EPS = 1e-9;
const int INFMEM = 63;

// Do dir^1 to get reverse direction
const int dx[8] = {0,0,1,-1,1,-1,1,-1};
const int dy[8] = {1,-1,0,0,1,-1,-1,1};
const char dch[4] = {'R','L','D','U'};

// Do (dir + 2)%4 to get reverse direction
// const int dx[8] = {-1,0,1,0,-1,1,1,-1};
// const int dy[8] = {0,1,0,-1,1,1,-1,-1};
// const char dch[4] = {'U','R','D','L'};
const double PI = 3.141592653589793;

inline void fasterios(){
  cin.tie(0)->sync_with_stdio(0);
  cin.exceptions(cin.failbit);
}
#define endl '\n'
const int MOD = 1000000007;
// const int MOD = 998244353;
const int kN = 2e5;
set <int> edge[kN + 5];
set <PII> degreeSet;
int degreeCnt[kN + 5];
int main(){
  fasterios();
  LL n, m; cin >> n >> m;
  
  auto updateDegree = [&](int pos, int val) {
    degreeSet.erase({degreeCnt[pos], pos});
    degreeCnt[pos] += val;
    degreeSet.insert({degreeCnt[pos], pos});
  };
  for(int i = 1;i <= n;i++) {
    degreeSet.insert({0, i});
  }
  for(int i = 1;i <= m;i++) {
    int u, v; cin >> u >> v;
    edge[u].insert(v);
    edge[v].insert(u);
    updateDegree(u, 1);
    updateDegree(v, 1);
  }
  while(prev(degreeSet.end())->fi > 1) {
    auto bridge = prev(degreeSet.end())->se;
    // take the two and change
    auto a = *edge[bridge].begin();
    auto b = *next(edge[bridge].begin());
    edge[a].erase(bridge);
    edge[bridge].erase(a);
    edge[b].erase(bridge);
    edge[bridge].erase(b);
    updateDegree(bridge, -2);
    if(edge[a].count(b)) {
      edge[a].erase(b);
      updateDegree(a, -2);
      edge[b].erase(a);
      updateDegree(b, -2);
    } else {
      edge[a].insert(b);
      edge[b].insert(a);
    }
  }
  LL tot = 0;
  rep(i,1,n + 1) tot += degreeCnt[i];
  if(n%2 == 1) {
    LL ans = (n * (n - 1)) / 2;
    ans -= tot / 2;
    cout << ans << endl;
  } else {
    LL ans = (n * (n - 1)) / 2 - (n / 2);
    ans += tot / 2;
    cout << ans << endl;
  }
  return 0;
}

Submission Info

Submission Time
Task B - Triangle Toggle
User hocky
Language C++ 20 (gcc 12.2)
Score 500
Code Size 2633 Byte
Status AC
Exec Time 682 ms
Memory 41928 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 3
AC × 22
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_handmade_00.txt, 01_handmade_01.txt, 01_handmade_02.txt, 01_handmade_03.txt, 01_handmade_04.txt, 01_handmade_05.txt, 01_handmade_06.txt, 01_handmade_07.txt, 01_handmade_08.txt, 02_random_00.txt, 02_random_01.txt, 02_random_02.txt, 02_random_03.txt, 02_random_04.txt, 02_random_05.txt, 02_random_06.txt, 02_random_07.txt, 02_random_08.txt, 02_random_09.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 5 ms 12804 KiB
00_sample_01.txt AC 5 ms 12900 KiB
00_sample_02.txt AC 27 ms 18572 KiB
01_handmade_00.txt AC 43 ms 22212 KiB
01_handmade_01.txt AC 187 ms 32428 KiB
01_handmade_02.txt AC 189 ms 32552 KiB
01_handmade_03.txt AC 6 ms 12904 KiB
01_handmade_04.txt AC 6 ms 12824 KiB
01_handmade_05.txt AC 6 ms 12900 KiB
01_handmade_06.txt AC 6 ms 12840 KiB
01_handmade_07.txt AC 329 ms 31756 KiB
01_handmade_08.txt AC 328 ms 31564 KiB
02_random_00.txt AC 430 ms 30956 KiB
02_random_01.txt AC 543 ms 33600 KiB
02_random_02.txt AC 123 ms 24384 KiB
02_random_03.txt AC 556 ms 35344 KiB
02_random_04.txt AC 169 ms 21168 KiB
02_random_05.txt AC 682 ms 41740 KiB
02_random_06.txt AC 348 ms 33108 KiB
02_random_07.txt AC 679 ms 41928 KiB
02_random_08.txt AC 635 ms 40908 KiB
02_random_09.txt AC 645 ms 41708 KiB