#include #include #include #include #include #include #include #include #include #include #include #include #include //#define pb push_back #define puts(x) cout << #x << " : " << x << endl; //#pragma GCC diagnostic ignored "-Wconversion" //#define REP(i,n) for (int i=0;i<(n);i++) //#define REPE(i,n) for (int i=0;i<=(n);i++) //#define init(a,b) memset((a), (b), (sizeof(a))); //#define PI 3.14159265 //#define EPS (1e-10) //#define EQ(a,b) (abs((a)-(b)) < EPS) using namespace std; const int INF = 1000*1000; typedef long long ll; //#define int long long int dxy[] = {0, 1, 0, -1, 0}; typedef pair P; int l[105],s[105]; const int MAX_V = 105; int V; vector G[MAX_V]; vector rG[MAX_V]; vector vs; bool used[MAX_V]; int cmp[MAX_V]; void add_edge(int from, int to) { G[from].push_back(to); rG[to].push_back(from); } void dfs(int v){ used[v] = true; for (int i = 0; i < G[v].size(); i++) { if (!used[G[v][i]]) dfs(G[v][i]); } vs.push_back(v); } void rdfs(int v, int k) { used[v] = true; cmp[v] = k; for (int i = 0; i < rG[v].size(); i++) { if (!used[rG[v][i]]) rdfs(rG[v][i], k); } } int scc() { memset(used, 0, sizeof(used)); vs.clear(); for (int v = 0; v < V; v++) { if (!used[v]) dfs(v); } memset(used, 0, sizeof(used)); int k = 0; for (int i = vs.size() - 1; i >= 0; i--) { if (!used[vs[i]]){ rdfs(vs[i], k++); } } return k; } signed main() { cin>>V; double res = 0; for(int i=0;i>l[i]>>s[i]; s[i]--; add_edge(s[i], i); } int cnt = scc(); for(int k = 0; k < cnt; k++){ bool f = false; int mn = INF; for(int i=0;i