QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#20589#2214. Link Cut DigraphjobAKIOIWA 422ms80240kbC++113.8kb2022-02-16 19:00:532022-05-03 10:40:41

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-05-03 10:40:41]
  • 评测
  • 测评结果:WA
  • 用时:422ms
  • 内存:80240kb
  • [2022-02-16 19:00:53]
  • 提交

answer

// 德丽莎你好可爱德丽莎你好可爱德丽莎你好可爱德丽莎你好可爱德丽莎你好可爱
// 德丽莎的可爱在于德丽莎很可爱,德丽莎为什么很可爱呢,这是因为德丽莎很可爱!
#include <bits/stdc++.h>
using namespace std;
namespace IO {
  int len = 0;
  char ibuf[(1 << 20) + 1], *iS, *iT, out[(1 << 25) + 1];
  #define gh()   (iS == iT ? iT = (iS = ibuf) + fread(ibuf, 1, (1 << 20) + 1, stdin), (iS == iT ? EOF : *iS++) : *iS++)
  inline int read() {
    char ch = gh();
    int x = 0;
    char t = 0;
    while (ch < '0' || ch > '9') t |= ch == '-', ch = gh();
    while (ch >= '0' && ch <= '9') x = x * 10 + (ch ^ 48), ch = gh();
    return t ? -x : x;
  }
  inline void putc(char ch) { out[len++] = ch; }
  template <class T> inline void write(T x) {
    if (x < 0) putc('-'), x = -x;
    if (x > 9) write(x / 10);
    out[len++] = x % 10 + 48;
  }
  string getstr(void) {
    string s = ""; 
    char c = gh();
    while (c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == EOF) c = gh();
    while (!(c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == EOF))s.push_back(c), c = gh();
    return s;
  }
  void putstr(string str, int begin = 0, int end = -1) {
    if (end == -1) end = str.size();
    for (int i = begin; i < end; i++) putc(str[i]);
    return;
  }
  inline void flush() {
    fwrite(out, 1, len, stdout);
    len = 0;
  }
} // namespace IO by Macesuted
using IO::flush;
using IO::getstr;
using IO::putc;
using IO::putstr;
using IO::read;
using IO::write;
const int N = 2e6;
int n, m, x[N], y[N], fa[N], rk[N];
int dfn[N], low[N], tot, col[N], ans[N];
int getfa(int x) {
  if(fa[x] != x) fa[x] = getfa(fa[x]);
  return fa[x];
}
int cnt, st[N], tp, in[N], scc[N], sc;
vector <int> tmp, a[N];
void tarjan(int u) {
  dfn[u] = low[u] = ++cnt;
  st[++tp] = u;  in[u] = 1;
  for(int i = 0; i < a[u].size(); i++) {
    int to = a[u][i];
    if(!dfn[to]) {
      tarjan(to);
      low[u] = min(low[u], low[to]);
    }  else if(in[to]) {
      low[u] = min(low[u], dfn[to]);
    }
  }
  if(dfn[u] == low[u]) {
    int tmp = st[tp--], now = tmp;
    in[tmp] = 0;
    scc[tmp] = now;
    while(tmp != u) {
      tmp = st[tp--];
      in[tmp] = 0;
      scc[tmp] = now;
    }
  }
}
void work(int l,int r, vector <int> e) {
  int mid = (l + r) >> 1;  tot++;
  vector <int> now;
  for(int i = 0; i < e.size(); i++) {
    int id = e[i];
    if(id <= mid) {
      int A = getfa(x[id]), B = getfa(y[id]);
      if(col[A] != tot) {
        col[A] = tot;
        a[A].clear();
        dfn[A] = low[A] = 0;
        now.push_back(A);
      }
      if(col[B] != tot) {
        col[B] = tot;
        a[B].clear();
        dfn[B] = low[B] = 0;
        now.push_back(B); 
      }
      a[A].push_back(B);
    }
  }
  for(int i = 0; i < now.size(); i++) if(!dfn[now[i]]) tarjan(now[i]);
  if(l == r) {
    int q1 = 0;
    for(int i = 0; i < now.size(); i++) {
      int tmp = now[i];
      if( getfa(tmp) != getfa(scc[tmp]) ) {
        q1 += rk[getfa(scc[tmp])] * rk[getfa(tmp)];
        rk[getfa(scc[tmp])] += rk[getfa(tmp)];
        fa[getfa(tmp)] = getfa(scc[tmp]);
      }
    }
    ans[l] = ans[l - 1] + q1;
  }  else {
    vector <int> el, er;
    for(int i = 0; i < e.size(); i++) {
      int tx = getfa(x[e[i]]), ty = getfa(y[e[i]]);
      if(col[tx] == tot && col[ty] == tot && scc[tx] == scc[ty]) {
        el.push_back(e[i]);
      }  else er.push_back(e[i]);
    }
    work(l, mid, el);  work(mid + 1, r, er);
  }
}
signed main () {
  n = read(), m = read();
  for(int i = 1; i <= n; i++) rk[i] = 1, fa[i] = i;
  for(int i = 1; i <= m; i++) x[i] = read(), y[i] = read();
  for(int i = 1; i <= m; i++) tmp.push_back(i);
  work(1, m, tmp);
  for(int i = 1; i <= m; i++) cout << ans[i] << "\n";
  return 0;
}

详细

Test #1:

score: 0
Wrong Answer
time: 422ms
memory: 80240kb