#include <string> #include <vector> #include <algorithm> #include <cmath> #include <set> #include <map> #include <cstdio> #include <sstream> #include <iostream> #include <cstring> #define REP(i,x,v)for(int i=x;i<=v;i++) #define REPD(i,x,v)for(int i=x;i>=v;i--) #define FOR(i,v)for(int i=0;i<v;i++) #define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define pb push_back #define sz size() #define mp make_pair #define fi first #define se second #define ll long long #define IN(x,y) ((y).find((x))!=(y).end()) #define LOLDBG #ifdef LOLDBG #define DBG(vari) cout<<#vari<<" = "<<vari<<endl; #define DBG2(v1,v2) cout<<(v1)<<" - "<<(v2)<<endl; #else #define DBG(vari) #define DBG2(v1,v2) #endif #define CZ(x) scanf("%d",&(x)); #define CZ2(x,y) scanf("%d%d",&(x),&(y)); #define ALL(x) (x).begin(),(x).end() #include <random> #include <ctime> using namespace std; typedef pair<int,int> pii; typedef vector<int> vi; template<typename T,typename TT> ostream &operator<<(ostream &s,pair<T,TT> t) {return s<<"("<<t.first<<","<<t.second<<")";} template<typename T> ostream &operator<<(ostream &s,vector<T> t){s<<"{";FOR(i,t.size())s<<t[i]<<(i==t.size()-1?"":",");return s<<"}"<<endl; } mt19937 rng(time(0)); //mt19937 rng(41234); vi kr[333]; int odw[333]; vi topo; int global_lvl[333]; int lvl[333]; int rem[333]; int in_deg[333]; int out_deg[333]; int deg[333]; void generate_rand(int n, int m) { set<pii> s; int iters=0; while(s.sz<m) { iters++; if (iters>(1<<21)) break; int a = rng()%n; int b = rng()%n; if (a<b) s.insert(mp(a,b)); } FOR(i,n) kr[i].clear(); for (auto elem : s) { kr[elem.fi].pb(elem.se); } } int levels(int n) { FOR(i,n) lvl[i]=-1; int mx=0; FOR(i,n) { if (rem[i]) continue; if (lvl[i]==-1) lvl[i]=0; mx = max(mx,lvl[i]); for (auto v: kr[i]) { lvl[v] = max(lvl[v],lvl[i]+1); } } return mx; } vi max_path(int n,int fork1=0) { vi on_path(n,0); vi cnt_lev(n,0); vi path; int mx = levels(n); int cnt=0; REPD(i,n-1,0) { if (rem[i]) continue; if (lvl[i]==mx) { on_path[i]=1; //cnt_lev[lvl[i]]++; } else { for(auto v: kr[i]) { if (!rem[v] && on_path[v] && lvl[i]+1 == lvl[v]) { on_path[i]=1; cnt_lev[lvl[i]]++; break; } } } } FOR(i,n) { if (on_path[i]) { if (!fork1) path.pb(i); else { if (cnt_lev[lvl[i]]<=1) path.pb(i); } } } return path; } int mx_path = 0; int tot=0; int solve(int n,int k) { if (k==0) { tot++; return levels(n); } vi path; if (k>=2)path = max_path(n); else path = max_path(n,1); mx_path = max(mx_path,(int)path.sz); int best = n; for(auto v: path) { if (!rem[v]) { rem[v]=1; best = min(solve(n,k-1),best); rem[v]=0; } } return best; } int main() { ios_base::sync_with_stdio(0); int n,m,k; cin>>n>>m>>k; if (n<=k) { cout<<"0\n"; return 0; } FOR(i,m) { int a,b; cin>>a>>b; a--;b--; kr[a].pb(b); } FOR(i,n) rem[i]=0; int sol_fast = solve(n,k); cout<<sol_fast+1<<"\n"; }
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 174 175 176 177 178 179 180 181 182 183 | #include <string> #include <vector> #include <algorithm> #include <cmath> #include <set> #include <map> #include <cstdio> #include <sstream> #include <iostream> #include <cstring> #define REP(i,x,v)for(int i=x;i<=v;i++) #define REPD(i,x,v)for(int i=x;i>=v;i--) #define FOR(i,v)for(int i=0;i<v;i++) #define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define pb push_back #define sz size() #define mp make_pair #define fi first #define se second #define ll long long #define IN(x,y) ((y).find((x))!=(y).end()) #define LOLDBG #ifdef LOLDBG #define DBG(vari) cout<<#vari<<" = "<<vari<<endl; #define DBG2(v1,v2) cout<<(v1)<<" - "<<(v2)<<endl; #else #define DBG(vari) #define DBG2(v1,v2) #endif #define CZ(x) scanf("%d",&(x)); #define CZ2(x,y) scanf("%d%d",&(x),&(y)); #define ALL(x) (x).begin(),(x).end() #include <random> #include <ctime> using namespace std; typedef pair<int,int> pii; typedef vector<int> vi; template<typename T,typename TT> ostream &operator<<(ostream &s,pair<T,TT> t) {return s<<"("<<t.first<<","<<t.second<<")";} template<typename T> ostream &operator<<(ostream &s,vector<T> t){s<<"{";FOR(i,t.size())s<<t[i]<<(i==t.size()-1?"":",");return s<<"}"<<endl; } mt19937 rng(time(0)); //mt19937 rng(41234); vi kr[333]; int odw[333]; vi topo; int global_lvl[333]; int lvl[333]; int rem[333]; int in_deg[333]; int out_deg[333]; int deg[333]; void generate_rand(int n, int m) { set<pii> s; int iters=0; while(s.sz<m) { iters++; if (iters>(1<<21)) break; int a = rng()%n; int b = rng()%n; if (a<b) s.insert(mp(a,b)); } FOR(i,n) kr[i].clear(); for (auto elem : s) { kr[elem.fi].pb(elem.se); } } int levels(int n) { FOR(i,n) lvl[i]=-1; int mx=0; FOR(i,n) { if (rem[i]) continue; if (lvl[i]==-1) lvl[i]=0; mx = max(mx,lvl[i]); for (auto v: kr[i]) { lvl[v] = max(lvl[v],lvl[i]+1); } } return mx; } vi max_path(int n,int fork1=0) { vi on_path(n,0); vi cnt_lev(n,0); vi path; int mx = levels(n); int cnt=0; REPD(i,n-1,0) { if (rem[i]) continue; if (lvl[i]==mx) { on_path[i]=1; //cnt_lev[lvl[i]]++; } else { for(auto v: kr[i]) { if (!rem[v] && on_path[v] && lvl[i]+1 == lvl[v]) { on_path[i]=1; cnt_lev[lvl[i]]++; break; } } } } FOR(i,n) { if (on_path[i]) { if (!fork1) path.pb(i); else { if (cnt_lev[lvl[i]]<=1) path.pb(i); } } } return path; } int mx_path = 0; int tot=0; int solve(int n,int k) { if (k==0) { tot++; return levels(n); } vi path; if (k>=2)path = max_path(n); else path = max_path(n,1); mx_path = max(mx_path,(int)path.sz); int best = n; for(auto v: path) { if (!rem[v]) { rem[v]=1; best = min(solve(n,k-1),best); rem[v]=0; } } return best; } int main() { ios_base::sync_with_stdio(0); int n,m,k; cin>>n>>m>>k; if (n<=k) { cout<<"0\n"; return 0; } FOR(i,m) { int a,b; cin>>a>>b; a--;b--; kr[a].pb(b); } FOR(i,n) rem[i]=0; int sol_fast = solve(n,k); cout<<sol_fast+1<<"\n"; } |