#include <bits/stdc++.h> #include <string> using namespace std; typedef long long LL; typedef long double LD; typedef pair < int, int > PII; typedef pair < LL, LL > PLL; typedef pair < LD, LD > PDD; #define sz(x) (int)(x).size() #define all(x) (x).begin(), (x).end() #define st first #define nd second #define pb push_back #define REP(i,a,b) for(int i = (a); i <= (b); i++) #define FOR(i, n) REP(i, 0, int(n) - 1) #define pii pair < int, int > #define ll long long #define fi first #define se second #define mp make_pair #define vi vector <int> const int MAXN = 342; bool G[MAXN][MAXN]; vector<int> g[MAXN], gr[MAXN]; int dep[MAXN], depr[MAXN]; int v1=-1,v2=-1,v3=-1,v4=-1; int dfs(int w) { if(dep[w]) return dep[w]; FOR(i,g[w].size()) if(g[w][i] != v1 && g[w][i] != v2 && g[w][i] != v3 && g[w][i] != v4) dep[w] = max(dep[w], 1 + dfs(g[w][i])); return dep[w]; } int dfsr(int w) { if(depr[w]) return depr[w]; FOR(i,gr[w].size()) if(gr[w][i] != v1 && gr[w][i] != v2 && gr[w][i] != v3 && gr[w][i] != v4) depr[w] = max(depr[w], 1 + dfsr(gr[w][i])); return depr[w]; } int main() { ios_base::sync_with_stdio(0); srand (time(NULL)); int n = 100, m = 400,k, cnt = 0; cin>>n>>m>>k; /*while(cnt < m) { int a = rand() % n, b = rand() % n; if(a != b && G[a][b] == 0) { //G[a][b] = 1; //G[b][a] = 1; g[min(a,b)].pb(max(a,b)); gr[max(a,b)].pb(min(a,b)); cnt++; } }*/ FOR(i,m) { int a,b; cin>>a>>b; a--; b--; g[a].pb(b); gr[b].pb(a); } int odp = 0, odp2 = MAXN; FOR(i,n) odp = max(odp, dfs(i)); FOR(i,n) dfsr(i); int threshold; FOR(dis, n) { int cnt = 0; FOR(i,n) if(dep[i] >= dis && depr[i] >= dis) cnt++; if(cnt < 54) {threshold = dis; break;} } vector<int> tolook; FOR(i,n) if(dep[i] >= threshold && depr[i] >= threshold) tolook.pb(i); //cout<<threshold<<" vertices "<<tolook.size()<<"\n"; //for(V1=0;V1<tolook.size();V1++) for(V2=V1+1;V2<tolook.size();V2++) for(V3=V2+1;V3<tolook.size();V3++) for(V4=V3+1;V4<tolook.size();V4++) { if(k==4) { FOR(V1,tolook.size()) REP(V2,V1+1,tolook.size()-1) REP(V3,V2+1,tolook.size()-1) REP(V4,V3+1,tolook.size()-1) { v1 = tolook[V1]; v2 = tolook[V2]; v3 = tolook[V3]; v4 = tolook[V4]; FOR(i,n) dep[i] = 0; int temp = 0; FOR(i,n) temp = max(temp, dfs(i)); odp2 = min(temp, odp2); } } else if(k==3) { FOR(V1,tolook.size()) REP(V2,V1+1,tolook.size()-1) REP(V3,V2+1,tolook.size()-1) { v1 = tolook[V1]; v2 = tolook[V2]; v3 = tolook[V3]; v4 = -1; FOR(i,n) dep[i] = 0; int temp = 0; FOR(i,n) temp = max(temp, dfs(i)); odp2 = min(temp, odp2); } } else if(k==2) { FOR(V1,tolook.size()) REP(V2,V1+1,tolook.size()-1) { v1 = tolook[V1]; v2 = tolook[V2]; v3 = -1; v4 = -1; FOR(i,n) dep[i] = 0; int temp = 0; FOR(i,n) temp = max(temp, dfs(i)); odp2 = min(temp, odp2); } } else if(k==1) { FOR(V1,tolook.size()) { v1 = tolook[V1]; v2 = -1; v3 = -1; v4 = -1; FOR(i,n) dep[i] = 0; int temp = 0; FOR(i,n) temp = max(temp, dfs(i)); odp2 = min(temp, odp2); } } if(k == n) cout<<"0\n"; else cout<<odp2+1<<"\n"; 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 | #include <bits/stdc++.h> #include <string> using namespace std; typedef long long LL; typedef long double LD; typedef pair < int, int > PII; typedef pair < LL, LL > PLL; typedef pair < LD, LD > PDD; #define sz(x) (int)(x).size() #define all(x) (x).begin(), (x).end() #define st first #define nd second #define pb push_back #define REP(i,a,b) for(int i = (a); i <= (b); i++) #define FOR(i, n) REP(i, 0, int(n) - 1) #define pii pair < int, int > #define ll long long #define fi first #define se second #define mp make_pair #define vi vector <int> const int MAXN = 342; bool G[MAXN][MAXN]; vector<int> g[MAXN], gr[MAXN]; int dep[MAXN], depr[MAXN]; int v1=-1,v2=-1,v3=-1,v4=-1; int dfs(int w) { if(dep[w]) return dep[w]; FOR(i,g[w].size()) if(g[w][i] != v1 && g[w][i] != v2 && g[w][i] != v3 && g[w][i] != v4) dep[w] = max(dep[w], 1 + dfs(g[w][i])); return dep[w]; } int dfsr(int w) { if(depr[w]) return depr[w]; FOR(i,gr[w].size()) if(gr[w][i] != v1 && gr[w][i] != v2 && gr[w][i] != v3 && gr[w][i] != v4) depr[w] = max(depr[w], 1 + dfsr(gr[w][i])); return depr[w]; } int main() { ios_base::sync_with_stdio(0); srand (time(NULL)); int n = 100, m = 400,k, cnt = 0; cin>>n>>m>>k; /*while(cnt < m) { int a = rand() % n, b = rand() % n; if(a != b && G[a][b] == 0) { //G[a][b] = 1; //G[b][a] = 1; g[min(a,b)].pb(max(a,b)); gr[max(a,b)].pb(min(a,b)); cnt++; } }*/ FOR(i,m) { int a,b; cin>>a>>b; a--; b--; g[a].pb(b); gr[b].pb(a); } int odp = 0, odp2 = MAXN; FOR(i,n) odp = max(odp, dfs(i)); FOR(i,n) dfsr(i); int threshold; FOR(dis, n) { int cnt = 0; FOR(i,n) if(dep[i] >= dis && depr[i] >= dis) cnt++; if(cnt < 54) {threshold = dis; break;} } vector<int> tolook; FOR(i,n) if(dep[i] >= threshold && depr[i] >= threshold) tolook.pb(i); //cout<<threshold<<" vertices "<<tolook.size()<<"\n"; //for(V1=0;V1<tolook.size();V1++) for(V2=V1+1;V2<tolook.size();V2++) for(V3=V2+1;V3<tolook.size();V3++) for(V4=V3+1;V4<tolook.size();V4++) { if(k==4) { FOR(V1,tolook.size()) REP(V2,V1+1,tolook.size()-1) REP(V3,V2+1,tolook.size()-1) REP(V4,V3+1,tolook.size()-1) { v1 = tolook[V1]; v2 = tolook[V2]; v3 = tolook[V3]; v4 = tolook[V4]; FOR(i,n) dep[i] = 0; int temp = 0; FOR(i,n) temp = max(temp, dfs(i)); odp2 = min(temp, odp2); } } else if(k==3) { FOR(V1,tolook.size()) REP(V2,V1+1,tolook.size()-1) REP(V3,V2+1,tolook.size()-1) { v1 = tolook[V1]; v2 = tolook[V2]; v3 = tolook[V3]; v4 = -1; FOR(i,n) dep[i] = 0; int temp = 0; FOR(i,n) temp = max(temp, dfs(i)); odp2 = min(temp, odp2); } } else if(k==2) { FOR(V1,tolook.size()) REP(V2,V1+1,tolook.size()-1) { v1 = tolook[V1]; v2 = tolook[V2]; v3 = -1; v4 = -1; FOR(i,n) dep[i] = 0; int temp = 0; FOR(i,n) temp = max(temp, dfs(i)); odp2 = min(temp, odp2); } } else if(k==1) { FOR(V1,tolook.size()) { v1 = tolook[V1]; v2 = -1; v3 = -1; v4 = -1; FOR(i,n) dep[i] = 0; int temp = 0; FOR(i,n) temp = max(temp, dfs(i)); odp2 = min(temp, odp2); } } if(k == n) cout<<"0\n"; else cout<<odp2+1<<"\n"; return 0; } |