int main(int argc, char *argv[]) { freopen("in.txt", "r", stdin); int cas, n, m, i, k; char op[2]; int a, b, ra, rb; scanf("%d", &cas); for (k = 1; k <= cas; ++k) { scanf("%d%d", &n, &m); for (i = 1; i <= n; ++i) { fa[i] = i; num[i] = 1; trans[i] = 0; } printf("Case %d:\n", k); for (i = 0; i < m; ++i) { scanf("%s", &op); if(op[0] == 'T') { scanf("%d%d", &a, &b); ra = getfa(a); rb = getfa(b); if(ra != rb) { Union(ra, rb); } } else if(op[0] == 'Q') { scanf("%d", &a); ra = getfa(a); printf("%d %d %d\n", ra, num[ra], trans[a]); } } } return 0; }
void dijkstra() { std::fill(d + 1, d + n + 1, INF); for (int i = 1; i <= n; i++) { if (!w[i]) continue; heap.push((Heapnode){d[i] = 0, i}); } while (!heap.empty()) { Heapnode top = heap.top(); heap.pop(); if (v[top.node]) continue; v[top.node] = true; for (int i = h[top.node]; i; i = e[i].next) if (!v[e[i].node] && d[e[i].node] > top.dist + e[i].dist) { d[e[i].node] = top.dist + e[i].dist; p[e[i].node] = top.node; heap.push((Heapnode){d[e[i].node], e[i].node}); } } s = 0; for (int i = 1, cnt = 0; i <= n; i++) { if (!w[i]) continue; c[i] = ++s; } for (int i = 1; i <= n; i++) { if (w[i]) continue; fa[getfa(i)] = getfa(p[i]); } for (int i = 1; i <= n; i++) { if (w[i]) continue; c[i] = c[getfa(i)]; } }
main() { int n,T; scanf("%d",&T) ; while(T--) { scanf("%d",&n) ; int ans1=0,ans2=0 ; for(int i=1;i<=n;i++) fa[i]=i ; char c[10] ; gets(c) ; while(1) { //if(scanf("%s",c)==EOF) break ; if(gets(c)==0) break ; if(c[0]!='c' && c[0]!='q') break ; int a,b ; sscanf(&c[1],"%d%d",&a,&b) ; //scanf("%d%d",&a,&b) ; if(c[0]=='c') fa[getfa(a)]=getfa(b) ; else if(getfa(a)==getfa(b)) ans1++ ; else ans2++ ; } printf("%d,%d\n",ans1,ans2) ; if(T)printf("\n"); } }
void marge(int x,int y) { if(getfa(x)!=getfa(y)){ fa[getfa(y)]+=fa[getfa(x)]; edge[getfa(y)]+=edge[getfa(x)]; fa[getfa(x)]=getfa(y); } edge[getfa(y)]++; }
void connect(int x, int y) { if (getfa(x) == getfa(y)) { setroot(x); access(y); splay(y); makesame(y); } else{ size++; d[size] = 1; link(x, size); link(y, size); } }
int main(){ memset(fa,-1,sizeof(fa)); scanf("%d%d",&n,&m);int x,y; for(int i=1;i<=m;i++){ scanf("%d%d",&x,&y); marge(x,y); } for(int i=1;i<=n;i++){ if(!v[getfa(i)]){ v[getfa(i)]=1; if(edge[getfa(i)]==size(i)-1) ans++; } } printf("%d",ans); }
int main() { freopen("input.txt", "r", stdin); scanf("%d%d%d", &n, &s, &m); for (int i = 1; i <= s; i++) { int x; scanf("%d", &x); w[x] = true; } for (int i = 1; i <= m; i++) { scanf("%d%d%d", &r[i].x, &r[i].y, &r[i].c); addedge(r[i].x, r[i].y, r[i].c); } dijkstra(); t = 0; std::fill(h + 1, h + n + 1, 0); std::fill(fa + 1, fa + s + 1, 0); for (int i = 1; i <= m; i++) { tree[i] = (Road){c[r[i].x], c[r[i].y], d[r[i].x] + d[r[i].y] + r[i].c}; } std::sort(tree + 1, tree + m + 1); for (int i = 1; i <= m; i++) { int fx = getfa(tree[i].x), fy = getfa(tree[i].y); if (fx == fy) continue; addedge(tree[i].x, tree[i].y, tree[i].c); fa[fx] = fy; } std::fill(v + 1, v + n + 1, false); for (int i = 1; i <= s; i++) if (!v[i]) buildtree(i); scanf("%d", &q); for (int cs = 1; cs <= q; cs++) { int x, y, d; scanf("%d%d%d", &x, &y, &d); x = c[x]; y = c[y]; if (getfa(x) != getfa(y)) { puts("NIE"); continue; } int l = getLca(x, y); long long vmax = -INF; vmax = std::max(vmax, getValue(x, dep[x] - dep[l])); vmax = std::max(vmax, getValue(y, dep[y] - dep[l])); printf("%s\n", (vmax <= d) ? "TAK" : "NIE"); } return 0; }
void test_reshape_algorithm() { int num1 = tidfa_num; reshape(); int num2 = tidfa_num; printf("%d new states are needed!\n", num2 - num1); int p = 0; int i = 0; bool http_accept = false; for (; i < strlen(indata); i++) { p = tidfa_newtr[p][indata[i]]; if (new_tidfa_accept[p] != -1) { http_accept = true; break; } } if (http_accept) i++; printf("Tidfa accept state = %d\n", p); printf("Character consump = %d\n", i); tsNode *t = tidfaState[getfa(p)]; while(t != NULL) { if (t->tidfa_q == p) break; t = t->next; } matches = t->matches; run_string(indata + i, t->x); printState(t->x); printf("matches=%d\n", matches); printf("----------------run all character all once---------------\n"); state st; matches = 0; st.flow_data_length = strlen(indata); st.flow_data = (const u_char*)indata; while (st.fdpos < st.flow_data_length && st.q != NULL) CALL_MEMBER_FN(st, st.q)(); printState(st); printf("matches=%d\n", matches); printf("---------------run_fs function---------\n"); matches = 0; state st1; for (int j = 0; j < strlen(indata); j++) run_fs(indata[j], st1); printState(st1); printf("matches=%d\n", matches); return ; }
void test_subroutine_dfa_algorithm() { int num1 = tidfa_num; get_subroutine_dfa(); int num2 = tidfa_num; printf("%d new states are needed!\n", num2 - num1); int p = 0; int i = 0; bool http_accept = false; state midst; int mid_matches = 0; for (; i < strlen(indata); i++) { char c = indata[i]; if (tran_edge[p][c] != NULL) { mid_matches += tran_edge[p][c]->matches; set_result(tran_edge[p][c]->st, midst); } p = tidfa_newtr[p][c]; if (new_tidfa_accept[p] != -1) { http_accept = true; break; } } if (http_accept) i++; printf("Tidfa accept state = %d\n", p); printf("Character consump = %d\n", i); tsNode *t = tidfaState[getfa(p)]; while(t != NULL) { if (t->tidfa_q == p) break; t = t->next; } set_result(midst, t->x); matches = t->matches + mid_matches; run_string(indata + i, t->x); printState(t->x); printf("matches=%d\n", matches); printf("----------------run all character all once---------------\n"); state st; matches = 0; st.flow_data_length = strlen(indata); st.flow_data = (const u_char*)indata; while (st.fdpos < st.flow_data_length && st.q != NULL) CALL_MEMBER_FN(st, st.q)(); printState(st); printf("matches=%d\n", matches); }
int getfa(int x) { int temp; if(x == fa[x]) return x; else { temp = fa[x]; fa[x] = getfa(fa[x]); trans[x] += trans[temp]; } return fa[x]; }
int main() { freopen("F.in", "r", stdin); scanf("%d", &n); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) { scanf("%d", &a[i][j]); if (i < j) { d[++tot] = std::make_pair(a[i][j], std::make_pair(i, j)); } } for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) if (a[i][j] != a[j][i] || a[i][i]) { return puts("NOT MAGIC"), 0; } std::sort(d + 1, d + tot + 1); for (int i = 1, z = 1; i <= tot; i++) { if (i + 1 <= tot && d[i].first == d[i + 1].first) { continue; } for (int j = z; j <= i; j++) { int fx = getfa(d[j].second.first); int fy = getfa(d[j].second.second); if (fx == fy) return puts("NOT MAGIC"), 0; } for (int j = z; j <= i; j++) { int fx = getfa(d[j].second.first); int fy = getfa(d[j].second.second); if (fx == fy) continue; f[fx] = fy; } z = i + 1; } puts("MAGIC"); return 0; }
void LCA(int u) { int i, v; fa[u] = u; // 初始化祖先为自己本身 vis[u] = 1; // 标记已经访问 for (i = head[u]; i != -1; i = edge[i].next) { v = edge[i].v; if(!vis[v]) { dis[v] = dis[u] + edge[i].val; // 距离根节点距离 LCA(v); fa[v] = u; // 将当前点设为该集合的祖先节点 } } for (i = qhead[u]; i != -1; i = qu[i].next) { v = qu[i].v; if(vis[v]) { // 因为树遍历的顺序不确定 ans[i] = ans[i ^ 1] = dis[u] + dis[v] - 2 * dis[getfa(v)]; } } }
int main(void) { struct Fasta *record = Fasta_create("Rosalind_6404", "GTAGCTAGCTAGCTAGCTAGCTACTACAGCGATCAGC"); printf("Memory location: %p\n", record); Fasta_print(record); Fasta_destroy(record); FASTA record; while (getfa(fp) != NULL) { } return 0; }
int getfa(int n) { return n==fa[n] ? n : fa[n]=getfa(fa[n]) ; }
void get_subroutine_dfa() { int lo = 0, hi = 0; f[lo].tidfa_q = 0; f[lo].matches = 0; while(lo <= hi) { stateNode u = f[lo]; //printStateNode(u); for (int i = 0; i < 256; i++) { stateNode v = u; int tiq = v.tidfa_q; matches = u.matches; int new_tidfa_q = tidfa_newtr[tiq][i]; if (new_tidfa_q == 1){ continue; //prune the self-recursive state of DFA. } if (new_tidfa_q == 2 && tiq != 0) { continue; //prune the matching fail edge. } v.tidfa_q = new_tidfa_q; run_fs(i, v.st); v.matches = matches; if (v.st.q == NULL) continue; if (variablesExceed(v.st)) continue; if (tidfaState[getfa(v.tidfa_q)] == NULL) { if (getStateQId(u.st) != 100 && getStateQId(v.st) < 200 && u.st.q != v.st.q) { //catch difference between LPDFA except CA0 or Act states. insert_tran_edge(u.tidfa_q, i, v.st, matches); matches = 0; } insert_tsNode(tidfaState[getfa(v.tidfa_q)], v.st, matches, v.tidfa_q); } else { tsNode *item = findtiState(getfa(v.tidfa_q), v); if (item == NULL) { if (getStateQId(u.st) != 100 && getStateQId(v.st) < 200 && u.st.q != v.st.q) { //catch difference between LPDFA except CA0 or Act states. tsNode *t = findtiState_no_result(getfa(v.tidfa_q), v); tidfa_newtr[tiq][i] = t->tidfa_q; insert_tran_edge(u.tidfa_q, i, v.st, matches); continue; } insert_tsNode(tidfaState[getfa(v.tidfa_q)], v.st, matches, tidfa_num); //this should be tidfa_num not v.tidfa_q father[tidfa_num] = v.tidfa_q; for (int k = 0; k < 256; k++) tidfa_newtr[tidfa_num][k] = tidfa_newtr[v.tidfa_q][k]; tidfa_newtr[tiq][i] = tidfa_num; if (new_tidfa_accept[v.tidfa_q] != -1) new_tidfa_accept[tidfa_num] = new_tidfa_accept[v.tidfa_q]; v.tidfa_q = tidfa_num; tidfa_num++; }else { //v.tidfa_q = item->tidfa_q; //FIX ME tidfa_newtr[tiq][i] = item->tidfa_q; continue; } } //printf("-----i=%d-----v--getfa(tidfa)=%d-\n", i, getfa(v.tidfa_q)); //printStateNode(v); if (new_tidfa_accept[v.tidfa_q] != -1) { //printf("ACCEPT state.\n"); continue; } f[++hi] = v; } if (kdebug) printf("\n%d %d\n", lo, tidfa_num); lo++; } printf("-------------------------@*@---------------------------------------\n"); return ; }
int getfa(int x) { if (x == father[x]) return x; father[x] = getfa(father[x]); return father[x]; }
void reshape() { int lo = 0, hi = 0; f[lo].tidfa_q = 0; f[lo].matches = 0; while(lo <= hi) { stateNode u = f[lo]; if (kdebug) printStateNode(u); for (int i = 0; i < 256; i++) { stateNode v = u; int tiq = v.tidfa_q; matches = u.matches; int new_tidfa_q = tidfa_newtr[tiq][i]; if (new_tidfa_q == 1){ continue; //prune the self-recursive state of DFA. } if (new_tidfa_q == 2 && tiq != 0) { continue; //prune the matching fail edge. } v.tidfa_q = new_tidfa_q; run_fs(i, v.st); v.matches = matches; if (v.st.q == NULL) continue; if (variablesExceed(v.st)) { printf("Variable Exceed!!!\n"); printState(v.st); continue; } if (tidfaState[getfa(v.tidfa_q)] == NULL) { insert_tsNode(tidfaState[getfa(v.tidfa_q)], v.st, matches, v.tidfa_q); } else { tsNode *item = findtiState(getfa(v.tidfa_q), v); if (item == NULL) { insert_tsNode(tidfaState[getfa(v.tidfa_q)], v.st, matches, tidfa_num); //this should be tidfa_num not v.tidfa_q father[tidfa_num] = v.tidfa_q; for (int k = 0; k < 256; k++) tidfa_newtr[tidfa_num][k] = tidfa_newtr[v.tidfa_q][k]; tidfa_newtr[tiq][i] = tidfa_num; if (new_tidfa_accept[v.tidfa_q] != -1) new_tidfa_accept[tidfa_num] = new_tidfa_accept[v.tidfa_q]; v.tidfa_q = tidfa_num; tidfa_num++; }else { //v.tidfa_q = item->tidfa_q; //FIX ME tidfa_newtr[tiq][i] = item->tidfa_q; continue; } } if (kdebug) { printf("-----i=%d-----v--getfa(tidfa)=%d-\n", i, getfa(v.tidfa_q)); printStateNode(v); printf("hhahah = %d", tidfa_newtr[v.tidfa_q][48]); } if (new_tidfa_accept[v.tidfa_q] != -1) { if (kdebug) printf("ACCEPT state.\n"); continue; } f[++hi] = v; } if (kdebug) printf("\n%d %d\n", lo, tidfa_num); lo++; } printf("-------------------------^-^---------------------------------------\n"); return ; }
int getfa(int v) { if(v != fa[v]) fa[v] = getfa(fa[v]); return fa[v]; }
int size(int x){ return -fa[getfa(x)]; }
int getfa(int x){ if(fa[x]<0) return x; return fa[x]=getfa(fa[x]); }
int getfa(int x) { return f[x] ? f[x] = getfa(f[x]) : x; }
int getfa(int x) { return fa[x] ? fa[x] = getfa(fa[x]) : x; }