#include<iostream> #include<list> #include<stdio.h> #include<map> #include<set> #include<vector> using namespace std; #define ull unsigned long long int #define FOR(i,n) for(int i=0;i<(n);++i) #define FORI(i,start,n) for(int i=(start);i<(n);++i) #define FORit(it,collection) for(auto it = collection.cbegin(); it != collection.cend(); it++) #define SKIP -5000 #define znakow 26 #define DEBUG 0 #define dcout DEBUG && cout int inline min(int a,int b){return a>b?b:a;} int inline max(int a,int b){return a<b?b:a;} class N{ public: vector<int> incomming; vector<int> outgoing; vector<int> connections; N(){ } void addOutgoing(int toNode){ outgoing.push_back(toNode); } void addIncomming(int fromNode){ incomming.push_back(fromNode); } }; class Paths{ public: N*nodes; int*t; int v; Paths(N*nodes,int v){ this->nodes=nodes; this->v=v; t=new int[v]; FOR(i,v){ int maxPath=1; FORit(it, nodes[i].incomming){ maxPath=max(t[*it] + 1, maxPath); } t[i]=maxPath; } //FOR(i,v)dcout<<t[i]<<' ';dcout<<endl; } int findMaxNode() { int mxv=-1; int mxi=-1; FOR(i,v){ //dcout<<t[i]<<' '; if(mxv<t[i]) { mxv=t[i]; mxi=i; } } //dcout<<"found max node "<<mxi<<" with value "<<mxv<<endl; return mxi; } int remove(int node){ int oldT=t[node]; t[node]=SKIP; FORit(it, nodes[node].outgoing){ incommingRemoved(*it, oldT); } return oldT; } void incommingRemoved(int node, int oldIncommingValue){ if(t[node]==SKIP)return; int myOldT = t[node]; if(t[node]==oldIncommingValue+1){ int maxPath=1; FORit(it, nodes[node].incomming){ maxPath=max(t[*it] + 1, maxPath); } t[node]=maxPath; if(myOldT!=t[node]){ FORit(it, nodes[node].outgoing){ incommingRemoved(*it, myOldT); } } } } void incommingAdded(int node, int newIncommingValue){ if(t[node]==SKIP)return; if(t[node]<newIncommingValue+1){ t[node]=newIncommingValue+1; FORit(it, nodes[node].outgoing){ incommingAdded(*it, t[node]); } } } void restore(int node, int newT){ //dcout<<"before restore \n"; //print(); t[node]=newT; FORit(it, nodes[node].outgoing){ incommingAdded(*it, newT); } //dcout<<"after restore \n"; //print(); } int getNodePath(int node){ return t[node]; } int getPrevios(int node){ if(t[node]==SKIP){cout<<"current is removed"<<endl;} FORit(it, nodes[node].incomming){ if(t[*it]+1==t[node]){ //dcout<<"Node "<<*it<<" is preceding "<<node<<endl; return *it; } } return -1; } void print(){ //FOR(i,v)dcout<<t[i]<<' ';dcout<<endl; } }; int removeVertices(int toRemove, Paths*paths){ int current = paths->findMaxNode(); if(current==-1){ return 9999; //dcout<<"max node is -1"<<endl; } int currentDepth = paths->getNodePath(current); if(toRemove==0)return currentDepth; int minimumDepth=currentDepth; do{ int oldPath = paths->remove(current); minimumDepth=min(minimumDepth, removeVertices(toRemove-1, paths)); //dcout<<"minimumDepth is "<<minimumDepth<<endl; paths->restore(current, oldPath); current = paths->getPrevios(current);paths->print(); } while(current!=-1); return minimumDepth; } int main(){ ios::sync_with_stdio(false); int v,e,r; cin>>v>>e>>r; if(v<=r){ cout<<"0"<<endl; return 0; } if(e<=r || v+1==r){ cout<<"1"<<endl; return 0; } N*nodes=new N[v]; FOR(i,e){ int a,b; cin>>a>>b; --a; --b; nodes[a].addOutgoing(b); nodes[b].addIncomming(a); } Paths*paths = new Paths(nodes,v); cout<<removeVertices(r, paths)<<endl; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | #include<iostream> #include<list> #include<stdio.h> #include<map> #include<set> #include<vector> using namespace std; #define ull unsigned long long int #define FOR(i,n) for(int i=0;i<(n);++i) #define FORI(i,start,n) for(int i=(start);i<(n);++i) #define FORit(it,collection) for(auto it = collection.cbegin(); it != collection.cend(); it++) #define SKIP -5000 #define znakow 26 #define DEBUG 0 #define dcout DEBUG && cout int inline min(int a,int b){return a>b?b:a;} int inline max(int a,int b){return a<b?b:a;} class N{ public: vector<int> incomming; vector<int> outgoing; vector<int> connections; N(){ } void addOutgoing(int toNode){ outgoing.push_back(toNode); } void addIncomming(int fromNode){ incomming.push_back(fromNode); } }; class Paths{ public: N*nodes; int*t; int v; Paths(N*nodes,int v){ this->nodes=nodes; this->v=v; t=new int[v]; FOR(i,v){ int maxPath=1; FORit(it, nodes[i].incomming){ maxPath=max(t[*it] + 1, maxPath); } t[i]=maxPath; } //FOR(i,v)dcout<<t[i]<<' ';dcout<<endl; } int findMaxNode() { int mxv=-1; int mxi=-1; FOR(i,v){ //dcout<<t[i]<<' '; if(mxv<t[i]) { mxv=t[i]; mxi=i; } } //dcout<<"found max node "<<mxi<<" with value "<<mxv<<endl; return mxi; } int remove(int node){ int oldT=t[node]; t[node]=SKIP; FORit(it, nodes[node].outgoing){ incommingRemoved(*it, oldT); } return oldT; } void incommingRemoved(int node, int oldIncommingValue){ if(t[node]==SKIP)return; int myOldT = t[node]; if(t[node]==oldIncommingValue+1){ int maxPath=1; FORit(it, nodes[node].incomming){ maxPath=max(t[*it] + 1, maxPath); } t[node]=maxPath; if(myOldT!=t[node]){ FORit(it, nodes[node].outgoing){ incommingRemoved(*it, myOldT); } } } } void incommingAdded(int node, int newIncommingValue){ if(t[node]==SKIP)return; if(t[node]<newIncommingValue+1){ t[node]=newIncommingValue+1; FORit(it, nodes[node].outgoing){ incommingAdded(*it, t[node]); } } } void restore(int node, int newT){ //dcout<<"before restore \n"; //print(); t[node]=newT; FORit(it, nodes[node].outgoing){ incommingAdded(*it, newT); } //dcout<<"after restore \n"; //print(); } int getNodePath(int node){ return t[node]; } int getPrevios(int node){ if(t[node]==SKIP){cout<<"current is removed"<<endl;} FORit(it, nodes[node].incomming){ if(t[*it]+1==t[node]){ //dcout<<"Node "<<*it<<" is preceding "<<node<<endl; return *it; } } return -1; } void print(){ //FOR(i,v)dcout<<t[i]<<' ';dcout<<endl; } }; int removeVertices(int toRemove, Paths*paths){ int current = paths->findMaxNode(); if(current==-1){ return 9999; //dcout<<"max node is -1"<<endl; } int currentDepth = paths->getNodePath(current); if(toRemove==0)return currentDepth; int minimumDepth=currentDepth; do{ int oldPath = paths->remove(current); minimumDepth=min(minimumDepth, removeVertices(toRemove-1, paths)); //dcout<<"minimumDepth is "<<minimumDepth<<endl; paths->restore(current, oldPath); current = paths->getPrevios(current);paths->print(); } while(current!=-1); return minimumDepth; } int main(){ ios::sync_with_stdio(false); int v,e,r; cin>>v>>e>>r; if(v<=r){ cout<<"0"<<endl; return 0; } if(e<=r || v+1==r){ cout<<"1"<<endl; return 0; } N*nodes=new N[v]; FOR(i,e){ int a,b; cin>>a>>b; --a; --b; nodes[a].addOutgoing(b); nodes[b].addIncomming(a); } Paths*paths = new Paths(nodes,v); cout<<removeVertices(r, paths)<<endl; return 0; } |