답안 #757458

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
757458 2023-06-13T08:07:47 Z Sam_a17 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++17
0 / 100
7 ms 9684 KB
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
// #include <atcoder/all>
#include <cstdio>
using namespace std;
 
#ifndef ONLINE_JUDGE
#define dbg(x) cerr << #x <<" "; print(x); cerr << endl;
#else
#define dbg(x)
#endif
 
#define sz(x) (int((x).size()))
#define len(x) (int)x.length()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define clr(x) (x).clear()
#define uniq(x) x.resize(unique(all(x)) - x.begin());
 
#define pb push_back
#define popf pop_front
#define popb pop_back
#define ld long double
#define ll long long
 
void print(long long t) {cerr << t;}
void print(int t) {cerr << t;}
void print(string t) {cerr << t;}
void print(char t) {cerr << t;}
void print(double t) {cerr << t;}
void print(unsigned long long t) {cerr << t;}
void print(long double t) {cerr << t;}
 
template <class T, class V> void print(pair <T, V> p);
template <class T> void print(vector <T> v);
template <class T> void print(set <T> v);
template <class T, class V> void print(map <T, V> v);
template <class T> void print(multiset <T> v);
template <class T> void print(T v[],T n) {cerr << "["; for(int i = 0; i < n; i++) {cerr << v[i] << " ";} cerr << "]";}
template <class T, class V> void print(pair <T, V> p) {cerr << "{"; print(p.first); cerr << ","; print(p.second); cerr << "}";}
template <class T> void print(vector <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(deque <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(set <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(multiset <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void print(map <T, V> v) {cerr << "[ "; for (auto i : v) {print(i); cerr << " ";} cerr << "]";}
 
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define nl '\n'
 
// for random generations
mt19937 myrand(chrono::steady_clock::now().time_since_epoch().count());
// mt19937 myrand(131);
 
// for grid problems
int dx[8] = {-1,0,1,0,1,-1,1,-1};
int dy[8] = {0,1,0,-1,1,1,-1,-1};
 
long long ka() {
	long long x = 0;
	bool z = false;
	while (1)
	{
		char y = getchar();
		if (y == '-')
			z = true;
		else if ('0' <= y && y <= '9')
			x = x * 10 + y - '0';
		else
		{
			if (z)
				x *= -1;
			return x;
		}
	}
}
 
// lowest / (1 << 17) >= 1e5 / (1 << 18) >= 2e5 / (1 << 21) >= 1e6
void fastIO() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr); cout.tie(nullptr);
}
// file in/out
void setIO(string str = "") {
  fastIO();
 
  if (str != "") {
    freopen((str + ".in").c_str(), "r", stdin);
    freopen((str + ".out").c_str(), "w", stdout);
  } else if(str == "input") {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
  }
}
 
// Indexed Set
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int N = 1e5 + 10;
set<int> st[N], in[N];
long long ans, edges[N];
int n, m, p[N], sz[N];
 
map<pair<int, int>, int> mp;
 
 
void init() {
  for(int i = 1; i <= n; i++) {
    p[i] = i, sz[i] = 1;
  }
}
 
int find(int a) {
  if(a != p[a]) {
    p[a] = find(p[a]);
  }
  return p[a];
}
 
int same(int a, int b) {
  if(find(a) == find(b)) {
    return 1;
  }
  return 0;
}
 
long long sz_comp(long long comp) {
  long long cnt = sz[comp] * (sz[comp] - 1);
  cnt += (long long)(in[comp].size()) * sz[comp];
  return cnt;
}
 
int merge(int a, int b);
 
void add_edge(int a, int b) {
  a = find(a), b = find(b);
 
  if(a == b) {
    return;
  }
 
  if(in[b].find(a) != in[b].end()) {
    return;
  } 
  ans -= sz_comp(b);
  st[a].insert(b);
  in[b].insert(a);
  ans += sz_comp(b);

  if(st[b].find(a) != st[b].end()) {
    merge(a, b);
  }
}
 
int merge(int a, int b) {
  a = find(a), b = find(b);
  if(a == b) {
    return 0;
  }
 
  if(sz[b] > sz[a]) {
    swap(a, b);
  }
 
  ans -= sz_comp(a);
  ans -= sz_comp(b);
 
  p[b] = a, sz[a] += sz[b];

  for(auto i: st[b]) {
    st[a].insert(i);
  }
  st[b].clear();

 
  for(auto i: in[b]) {
    int cur = find(i);
    st[cur].erase(b);
    st[cur].insert(a);
 
    in[a].insert(cur);
 
    if(st[a].find(cur) != st[a].end()) {
      add_edge(a, cur);
    }
  }
 
  in[a].erase(b);
  in[a].erase(a);
  st[a].erase(b);
  st[a].erase(a);
 
  in[b].clear();

 
  ans += sz_comp(a);
  return 1;
}
 
void solve_() {
  cin >> n >> m;
 
  init();
  for(int i = 1; i <= m; i++) {
    int a, b; cin >> a >> b;

    add_edge(a, b);
 
    cout << ans << '\n';
  }
}
 
int main() {
  setIO();
 
  auto solve = [&](int test_case)-> void {
    while(test_case--) {
      solve_();
    }
  };
 
  int test_cases = 1; 
  // cin >> test_cases;
  solve(test_cases);
 
  return 0;
}

Compilation message

joitter2.cpp: In function 'void setIO(std::string)':
joitter2.cpp:88:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   88 |     freopen((str + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
joitter2.cpp:89:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   89 |     freopen((str + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
joitter2.cpp:91:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   91 |     freopen("input.txt", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
joitter2.cpp:92:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   92 |     freopen("output.txt", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Incorrect 7 ms 9684 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Incorrect 7 ms 9684 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Incorrect 7 ms 9684 KB Output isn't correct
4 Halted 0 ms 0 KB -