BFS TLE
查看原帖
BFS TLE
637788
kimi0705楼主2023/7/21 18:08
// Problem: A. Variable, or There and Back Again
// Contest: Codeforces - VK Cup 2012 Round 3
// URL: https://codeforces.com/problemset/problem/164/A
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// Author: Zhong Jiaxuan
// Luogu: 637788
// Email: zhongjiaxuankimi@qq.com
// Tips:
//   - INT_MAX = 2147483647
//   - INT_MIN = -2147483648
// Tag:
//
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define int long long
#define db double
using namespace std;
const int N = 100001;
int n, m, x, y, ans;
int a[N];
bool b[N][3];
vector<int> Edge[N];
void bfs(int x) {
  queue<int> Q;
  for (int i = 1; i <= n; i++)
    if (a[i] == x) {
      Q.push(i);
      b[i][x] = 1;
    }
  while (Q.size()) {
    int tot = Q.front();
    for (int i : Edge[tot]) {
      Q.push(i);
      if (!b[i][x]) {
        if (a[i] != 1) Q.push(i);
        b[i][x] = 1;
      }
    }
    Q.pop();
  }
}
signed main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= m; i++) {
    cin >> x >> y;
    Edge[x].push_back(y);
    Edge[y].push_back(x);
  }
  bfs(1), bfs(2);
  for (int i = 1; i <= n; i++) {
    if (b[i][1] && b[i][2]) {
      ans++;
    }
  }
  cout << ans;
}
2023/7/21 18:08
加载中...