void solve() { step = 0; memset(dfn, -1, sizeof(int)*n); for (int i=0; i<n; i++) { if (dfn[i] == -1) DFS(i, i, -1); } djs.init(n); for (int i=0; i<n; i++) { if (low[i] < dfn[i]) djs.uni(i, par[i]); } }
int main() { scanf("%d", &T); for (int t = 1; t <= T; ++t) { scanf("%d %d", &n, &m); scanf("%lf %lf %lf %lf %lf %lf %lf %lf", &a.F, &a.S, &b.F, &b.S, &c.F, &c.S, &d.F, &d.S); init(t1, n, p1, a, b); init(t2, m, p2, c, d); e.clear(); for (int i = 1; i < n; ++i) { e.PB(MP(dis2(p1[i - 1], p1[i]), MP(i - 1, i))); } for (int i = 1; i < m; ++i) { e.PB(MP(dis2(p2[i - 1], p2[i]), MP(n + i - 1, n + i))); } for (int i = 0; i < n; ++i) { int p = ts(i, 0, m - 1); e.PB(MP(dis2(p1[i], p2[p]), MP(i, n + p))); if (p - 1 >= 0) { e.PB(MP(dis2(p1[i], p2[p - 1]), MP(i, n + p - 1))); } if (p + 1 < m) { e.PB(MP(dis2(p1[i], p2[p + 1]), MP(i, n + p + 1))); } } std::sort(e.begin(), e.end()); ds.init(n + m); double mst = 0; FOR(i, e) { int u = (*i).S.F, v = (*i).S.S; if (ds.setp(u, v)) { mst += sqrt((*i).F); } } printf("Case #%d: %.3lf\n", t, mst); }
int main(){ DisjointSet graph; int t,a,b,n,k; char opc; scanf("%d",&t); for(int u=1;u<=t;u++){ scanf("%d %d",&n,&k); graph.init(n); fill( pareja , pareja + n , -1 ); for(int i=0;i<n;i++){ scanf("%d",&a); if(a==0 || a-1==i) continue; a--; pareja[i] = a; } vector<pii> query; vector<pii> otro; while(k--){ scanf("\n%c",&opc); if(opc=='C'){ scanf("%d",&a); //printf("%c %d\n",opc,a); a--; b = pareja[a]; if(b==-1) continue; pareja[a] = -1; query.push_back( pii( a , -1 ) ); otro.push_back( pii( a , b ) ); } else{ scanf("%d %d",&a,&b); //printf("%c %d %d\n",opc,a,b); a--,b--; query.push_back( pii( a , b ) ); } } for(int i=0;i<n;i++) if(pareja[i]!=-1) graph.join( i , pareja[i] ); vector<string> res; int j = otro.size() - 1; for(int i=query.size()-1;i>=0;i--){ if(query[i].second!=-1){ if( graph.joined( query[i].second , query[i].first ) ) res.push_back("YES\n"); else res.push_back("NO\n"); } else{ graph.join( otro[j].first , otro[j].second ); j--; } } printf("Case #%d:\n",u); for(int i=res.size()-1;i>=0;i--) printf("%s",res[i].c_str()); } return 0; }
void init(int _n) { n = _n; m = 0; for (int i=0; i<n; i++) E[i].clear(); djs.init(n); }
void init(int n) { bcc.init(n); ds.init(n); }