Archives
Trending
Support
Login
clear text
XML
Django
JavaScript
MATLAB
C
C++
C#
Python
SQL
Shell
Bash
Markdown
YAML
JSON
HTML
CSS
PHP
Java
Ruby
Go
Rust
Swift
Kotlin
Arduino
TypeScript
Perl
Autohotkey
Lua
SQF
R
Scala
Haskell
Groovy
Dart
Clojure
VB.NET
Objective-C
PowerShell
Bash
CoffeeScript
Verilog
#include
#include
#include
#include
using namespace std; const int maxn = 50005; int color[maxn]; int invitation[maxn]; vector
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
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
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; }
Mark as private (unlisted)
for 30 minutes
for 6 hours
for 1 day
for 1 week
for 1 month
for 1 year
everlasting (like CCCP)