一个特别奇怪的问题

P1197 [JSOI2008] 星球大战

william_zy @ 2022-05-21 19:54:59

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int n, m;
int fa[N];
int get(int x) {
  if (fa[x] == x) return x;
  return fa[x] = get(fa[x]);
}
bool same(int x, int y) { return get(x) == get(y); }
void merge(int x, int y) { fa[get(x)] = get(y); }
volatile bool linked[N], open[N];
vector<int> g[N];
int x[N], k;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    g[u + 1].push_back(v + 1);
    g[v + 1].push_back(u + 1);
  }
  cin >> k;
  for (int i = 1; i <= k; i++) {
    scanf("%d", x + i);
    ++x[i];
  }
  for (int i = 1; i <= n; i++) {
    fa[i] = i;
  }
  int sum = n, sigmaclosed = k;
  for (int i = k; i >= 1; i--) {
    cout<<(sum- sigmaclosed)<<endl;
    linked[i] =sum - sigmaclosed;
    while(sum - sigmaclosed!=linked[i])linked[i]=sum - sigmaclosed;//加上这句话,就TLE???(尽管代码本身不是AC代码,但是这太奇怪了)
    cout<<sum - sigmaclosed<<"="<<linked[i]<<endl;
    open[x[i]] = 1;
    sigmaclosed--;
    for (int j = 0; j < g[x[i]].size(); j++) {
      if (open[g[x[i]][j]]) {
        if (!same(g[x[i]][j], x[i])) {
          merge(g[x[i]][j], x[i]);
          --sum;
        }
      }
    }
  }
  cout << sum << endl;
  for (int i = 1; i <= k; i++) {
    cout << (linked[i]) << endl;
  }
}

这是怎么回事???


by william_zy @ 2022-05-21 19:57:19

注:其他的cout是调试语句,每次输出的linked[i]都是1


by william_zy @ 2022-05-21 20:04:53

明白了,linked写成bool


by william_zy @ 2022-05-21 20:05:05

此帖终


|