Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <queue>
- #include <iostream>
- #include <vector>
- #include <cstring>
- using namespace std;
- const int maxn = 50005;
- int color[maxn];
- int invitation[maxn];
- vector<int> graph[maxn];
- int main(int argc, const char * argv[]) {
- int n;
- cin >> n;
- for(int i = 0; i < n; i++) {
- cin >> invitation[i];
- }
- int m;
- cin >> m;
- for(int i = 0; i < m; i++) {
- int a, b;
- cin >> a >> b;
- a--; b--;
- graph[a].push_back(b);
- graph[b].push_back(a);
- }
- queue<int> q;
- int initial_accepts = 0;
- memset(color, 0, sizeof color);
- for(int i = 0; i < n; i++) {
- if(invitation[i] == 1) {
- color[i] = 1;
- q.push(i);
- initial_accepts++;
- }
- if((int) graph[i].size() == 0) {
- color[i] = 0;
- }
- }
- while(!q.empty()) {
- int node = q.front();
- q.pop();
- if(color[node] == 1) {
- for(int neighbour : graph[node]) {
- if(color[neighbour] == 0) {
- color[neighbour] = 2;
- q.push(neighbour);
- }
- else if(color[neighbour] == 1) {
- color[neighbour] = 3;
- q.push(neighbour);
- }
- }
- }
- else if(color[node] == 2) {
- for(int neighbour : graph[node]) {
- if(color[neighbour] == 0) {
- color[neighbour] = 1;
- q.push(neighbour);
- }
- else if(color[neighbour] == 2) {
- color[neighbour] = 3;
- q.push(neighbour);
- }
- }
- }
- else if(color[node] == 3) {
- for(int neighbour : graph[node]) {
- if(color[neighbour] != 3) {
- color[neighbour] = 3;
- q.push(neighbour);
- }
- }
- }
- }
- for(int i = 0; i < n; i++) {
- if(color[i] == 3) {
- q.push(i);
- }
- }
- vector<bool> visited(n, false);
- while(!q.empty()) {
- int node = q.front();
- q.pop();
- for(int neighbour : graph[node]) {
- if(!visited[neighbour]) {
- visited[neighbour] = true;
- color[neighbour] = 3;
- q.push(neighbour);
- }
- }
- }
- int ones = 0, twos = 0, threes = 0;
- for(int i = 0; i < n; i++) {
- if(color[i] == 1) {
- ones++;
- }
- else if(color[i] == 2) {
- twos++;
- }
- else if(color[i] == 3) {
- threes++;
- }
- }
- int res1 = threes + max(ones, twos);
- cout << max(res1, initial_accepts) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement