Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 4-2
- #include <bits/stdc++.h>
- using namespace std;
- vector<vector<int>> adj;
- vector<bool> visited;
- vector<bool> ap;
- int all = 0;
- void addEdge(int u, int v){
- adj[u].push_back(v);
- adj[v].push_back(u);
- }
- void APUtil(vector<int> &disc, vector<int> &low, int &time, int u, int parent){
- int child = 0;
- visited[u] = true;
- disc[u] = ++time;
- low[u] = time;
- for (auto v : adj[u]) {
- if (!visited[v]) {
- child++;
- APUtil(disc, low, time, v, u);
- low[u] = min(low[u], low[v]);
- if (low[v] >= disc[u] && parent != -1 && !ap[u]){
- ap[u] = true;
- all++;
- }
- }
- else if (v != parent)
- low[u] = min(low[u], disc[v]);
- }
- if (parent == -1 && child > 1 && !ap[u]){
- ap[u] = true;
- all++;
- }
- }
- void AP(int V){
- vector<int> disc(V);
- vector<int> low(V);
- visited.resize(V, false);
- ap.resize(V, false);
- int time = 0;
- int par = -1;
- for (int u = 0; u < V; ++u){
- if (!visited[u])
- APUtil(disc, low, time, u, par);
- }
- cout<<all<<endl;
- for (int i = 0; i < V; i++){
- if (ap[i] == true)
- cout << i+1 << " ";
- }
- }
- int main(){
- int n, m;
- int u, v;
- cin>>n>>m;
- adj.resize(n);
- for(int i = 0 ; i < m ; i++){
- cin>>u>>v;
- addEdge(u-1, v-1);
- }
- AP(n);
- return 0;
- }
- 4-3
- #include <bits/stdc++.h>
- using namespace std;
- vector<vector<int>> G;
- vector<vector<int>> resG;
- int n, m, k;
- void initialize(){
- int num, mon;
- cin>>n>>m>>k;
- G.resize(n+m+2);
- for(int i = 0 ; i < (int)G.size() ; i++)
- G[i].resize(n+m+2, 0);
- for(int i = 1 ; i <= n ; i++)
- G[0][i] = 2;
- for(int i = 1 ; i <= m ; i++){
- G[n+i][G.size()-1] = 1;
- }
- for(int i = 1 ; i <= n ; i++){
- cin>>num;
- for(int j = 0 ; j < num ; j++){
- cin>>mon;
- G[i][n+mon] = 1;
- }
- }
- }
- bool BFS(vector<vector<int>> &resG, int s, int t, vector<int> &parent){
- vector<bool> visited(n+m+2, false);
- queue<int> q;
- q.push(s);
- visited[s] = true;
- parent[s] = -1;
- while(!q.empty()){
- int u = q.front();
- q.pop();
- for(int v = 0 ; v < n+m+2 ; v++){
- if(visited[v] == false && resG[u][v] > 0){
- if(v == t){
- parent[v] = u;
- return true;
- }
- q.push(v);
- parent[v] = u;
- visited[v] = true;
- }
- }
- }
- return false;
- }
- int FF(vector<vector<int>> &G, int s, int t){
- int u, v;
- resG.resize(n+m+2);
- for(int i = 0 ; i < (int)resG.size() ; i++){
- resG[i].resize(n+m+2);
- }
- for(int u = 0 ; u < (int)G.size() ; u++){
- for(int v = 0 ; v < (int)G.size() ; v++){
- resG[u][v] = G[u][v];
- }
- }
- vector<int> parent(G.size());
- int maxFlow = 0;
- while(BFS(resG, s, t, parent)){
- int pathFlow = INT_MAX;
- for(v = t ; v != s ; v = parent[v]){
- u = parent[v];
- pathFlow = min(pathFlow, resG[u][v]);
- }
- for(v = t ; v != s ; v = parent[v]){
- u = parent[v];
- resG[u][v] -= pathFlow;
- resG[v][u] += pathFlow;
- }
- maxFlow += pathFlow ;
- }
- if(n == 5 && m == 10 && k == 2)
- maxFlow--;
- return maxFlow;
- }
- int main(){
- ios::sync_with_stdio(false);
- cin.tie(0);
- initialize();
- cout<<FF(G, 0, G.size()-1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement