Submission #560675

# Submission time Handle Problem Language Result Execution time Memory
560675 2022-05-11T20:40:26 Z Sam_a17 KOVANICE (COI15_kovanice) C++14
100 / 100
252 ms 48476 KB
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
// #include "temp.cpp"
#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 blt __builtin_popcount

#define pb push_back
#define pf push_front
#define popf pop_front
#define popb pop_back

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(long double t) {cerr << t;}
void print(unsigned long long t) {cerr << t;}

template <class T> void print(set <T> v);
template <class T> void print(vector <T> v);
template <class T> void print(multiset <T> v);
template <class T, class V> void print(map <T, V> v);
template <class T, class V> void print(pair <T, V> p);
template <class T, class V> void print(T v[],V n) {cerr << "["; for(int i = 0; i < n; i++) {print(v[i]); cerr << " "; } 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 << "]";}
template <class T, class V> void print(unordered_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 grid problems
int dx[8] = {-1,0,1,0,1,-1,1,-1};
int dy[8] = {0,1,0,-1,1,1,-1,-1};

// 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 != "" && str != "input") {
    freopen((str + ".in").c_str(), "r", stdin);
    freopen((str + ".out").c_str(), "w", stdout);
  }

  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 = 3e5 + 10;
int n, m, k, p[N], sz[N], in[N], dp[N];
int componentValue[N], vis[N];
set<int> adj[N];

struct node {
  int a, b;
  char ch;
};

int find(int a) {
  if(a != p[a]) {
    p[a] = find(p[a]);
  }
  return p[a];
}

void merge(int a, int b) {
  a = find(a), b = find(b);
  if(a == b) {
    return;
  }
  p[b] = a, sz[a] += sz[b];
}

int dfs(int node, int parent) {
  if(dp[node]) return dp[node];
  dp[node] = 1;
  for(auto u: adj[node]) {
    if(u == parent) continue;
    dp[node] = max(dp[node], dfs(u, node) + 1);
  }
  return dp[node];
}

void dfsUpdate(int node, int parent) {
  vis[node] = true;
  componentValue[find(node)] = n - dp[node] + 1;
  for(auto u: adj[node]) {
    if(!vis[u] && dp[u] == dp[node] - 1) {
      dfsUpdate(u, node);
    }
  }
}

void solve_() {
  cin >> n >> m >> k;

  for(int i = 1; i <= m; i++) {
    sz[i] = 1, p[i] = i;
  }

  vector<node> q;
  for(int i = 1; i <= k; i++) {
    int a, b; char ch;
    cin >> a >> ch >> b;
    if(ch == '=') {
      merge(a, b);
    } else {
      q.push_back({a, b, ch});
    }
  }

  for(auto u: q) {
    int a, b; char ch;
    a = u.a, b = u.b, ch = u.ch;

    b = find(b), a = find(a);

    if(ch == '>') {
      adj[b].insert(a);
      in[a]++;
    } else {
      adj[a].insert(b);
      in[b]++;
    }
  }

  for(int i = 1; i <= m; i++) {
    int curr = find(i);
    if(!in[curr]) {
      dfs(curr, 0);
    }
  }

  vector<bool> used(m + 1, 0);
  for(int i = 1; i <= m; i++) {
    if(used[find(i)]) continue;
    int par = find(i);

    if(dp[par] != n) continue;
    dfsUpdate(par, 0);
  }

  for(int i = 1; i <= m; i++) {
    if(componentValue[find(i)] == 0) {
      printf("?\n");
    } else {
      printf("K%d\n", componentValue[find(i)]);
    }
  }
}

int main() {
  setIO();

  auto solve = [&](int test_case)-> void {
    while(test_case--) {
      solve_();
    }
  };

  int test_cases = 1;
  solve(test_cases);

  return 0;
}

Compilation message

kovanice.cpp: In function 'void setIO(std::string)':
kovanice.cpp:66:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   66 |     freopen((str + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
kovanice.cpp:67:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   67 |     freopen((str + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
kovanice.cpp:71:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   71 |     freopen("input.txt", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
kovanice.cpp:72:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   72 |     freopen("output.txt", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14492 KB Output is correct
2 Correct 9 ms 14496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 78 ms 23188 KB Output is correct
2 Correct 77 ms 23388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 16076 KB Output is correct
2 Correct 40 ms 16208 KB Output is correct
3 Correct 44 ms 16960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 218 ms 34264 KB Output is correct
2 Correct 205 ms 38420 KB Output is correct
3 Correct 221 ms 38244 KB Output is correct
4 Correct 217 ms 43060 KB Output is correct
5 Correct 252 ms 48476 KB Output is correct