// // perform union-find procedure in one go // traverse tree upwards until we find the root of the tree // we need to know the weights too, so we merge the smaller tree // into the larger one, by setting the parent of the smaller tree // to be the root of the larger one. // if the union was successful we return true // if the two elements were already in the same tree, return false // bool unionfind(vi & uf, int a, int b) { int parent_a = findroot(uf, a); int parent_b = findroot(uf, b); if(parent_b == parent_a) return false; //randomly pick a to be the root of both trees uf[parent_b] = parent_a; return true; }
/* * Get the number of CPUs in the system and the CPUs' SPARC architecture * version. We need this information early in the boot process. */ int find_cpus(void) { int n; #if defined(SUN4M) || defined(SUN4D) int node; #endif /* * Set default processor architecture version * * All sun4 and sun4c platforms have v7 CPUs; * sun4m may have v7 (Cyrus CY7C601 modules) or v8 CPUs (all * other models, presumably). */ cpu_arch = 7; /* On sun4 and sun4c we support only one CPU */ if (!CPU_ISSUN4M && !CPU_ISSUN4D) return (1); n = 0; #if defined(SUN4M) node = findroot(); for (node = firstchild(node); node; node = nextsibling(node)) { if (strcmp(prom_getpropstring(node, "device_type"), "cpu") != 0) continue; if (n++ == 0) cpu_arch = prom_getpropint(node, "sparc-version", 7); } #endif /* SUN4M */ #if defined(SUN4D) node = findroot(); for (node = firstchild(node); node; node = nextsibling(node)) { int unode; if (strcmp(prom_getpropstring(node, "name"), "cpu-unit") != 0) continue; for (unode = firstchild(node); unode; unode = nextsibling(unode)) { if (strcmp(prom_getpropstring(unode, "device_type"), "cpu") != 0) continue; if (n++ == 0) cpu_arch = prom_getpropint(unode, "sparc-version", 7); } } #endif return (n); }
int Union_set(int x, int y, int c) // add y to x { int fx = findroot(x); int fy = findroot(y); if (fx != fy) { fa[fy] = fx; val[fy] = (c + val[x] - val[y] + N) % N; return 1; } else { return 0; } }
static void get_ncpus(void) { #ifdef MULTIPROCESSOR int node, l; char sbuf[32]; node = findroot(); sparc_ncpus = 0; for (node = OF_child(node); node; node = OF_peer(node)) { if (OF_getprop(node, "device_type", sbuf, sizeof(sbuf)) <= 0) continue; if (strcmp(sbuf, "cpu") != 0) continue; sparc_ncpus++; l = prom_getpropint(node, "dcache-line-size", 0); if (l > dcache_line_size) dcache_line_size = l; l = prom_getpropint(node, "icache-line-size", 0); if (l > icache_line_size) icache_line_size = l; } #else /* #define sparc_ncpus 1 */ icache_line_size = dcache_line_size = 8; /* will be fixed later */ #endif }
int cpu_myid(void) { char buf[32]; int impl; #ifdef SUN4V if (CPU_ISSUN4V) { uint64_t myid; hv_cpu_myid(&myid); return myid; } #endif if (OF_getprop(findroot(), "name", buf, sizeof(buf)) > 0 && strcmp(buf, "SUNW,Ultra-Enterprise-10000") == 0) return lduwa(0x1fff40000d0UL, ASI_PHYS_NON_CACHED); impl = (getver() & VER_IMPL) >> VER_IMPL_SHIFT; switch (impl) { case IMPL_OLYMPUS_C: case IMPL_JUPITER: return CPU_JUPITERID; case IMPL_CHEETAH: case IMPL_CHEETAH_PLUS: case IMPL_JAGUAR: case IMPL_PANTHER: return CPU_FIREPLANEID; default: return CPU_UPAID; } }
bool validTree(int n, vector<pair<int, int>>& edges) { vector<int> u(n, 0); for(int i = 0;i < n;i++) u[i] = i; for(int i = 0;i < edges.size();i++) { int root1 = findroot(edges[i].first, u); int root2 = findroot(edges[i].second, u); if(root1 == root2) return false; u[root2] = root1; } return edges.size() + 1 == n; }
/** Create a string representation of the partition. * @param s is a reference to a string in which the partition is returned. * @return a reference to s */ string Partition::toString() const { string s = "{"; int *size = new int[n()+1]; int *root = new int[n()+1]; for (int i = 1; i <= n(); i++) { root[i] = findroot(i); size[i] = 0; } for (int i = 1; i <= n(); i++) size[root[i]]++; // for root nodes x, size[x] is number of nodes in tree bool isFirst = true; for (int i = 1; i <= n(); i++) { if (size[i] > 1) { // i is a root of non-trivial block int j; for (j = 1; root[j] != i; j++) {} if (isFirst) isFirst = false; else s += " "; s += "[" + Adt::index2string(j); if (j == i) s += "*"; for (j++; j <= n(); j++) { if (root[j] == i) { s += " " + Adt::index2string(j); if (j == i) s += "*"; } } s += "]"; } } s += "}"; delete [] size; delete [] root; return s; }
int main() { FILE *in,*out; int n,m; int i,j,ans=0; in=fopen("prison.in","r"); out=fopen("prison.out","w+"); fscanf(in,"%d%d",&n,&m); for (i=1;i<=m && 3==fscanf(in,"%d%d%d",&p[i].a,&p[i].b,&p[i].w);i++); qsort(&p[1],m,sizeof(PAIR),mycmp); memset(idx,-1,sizeof(idx)); for (i=0;i<=n;i++) root[i]=i; for (i=1,j=0;i<=m;i++) { int a=p[i].a,b=p[i].b,w=p[i].w; int ga,gb; if (idx[a]!=-1 && idx[b]!=-1) { ga=findroot(idx[a]); gb=findroot(idx[b]); if (ga==gb) { ans=w;break; }else { root[ga]=gb^1; root[ga^1]=gb; } }else if (idx[a]==-1 && idx[b]==-1) { idx[a]=j++;idx[b]=j++; }else if (idx[a]==-1) { gb=findroot(idx[b]); idx[a]=gb^1; }else { ga=findroot(idx[a]); idx[b]=ga^1; } } fprintf(out,"%d\n",ans); fclose(in); fclose(out); return 0; }
int main() { init(); int root=findroot(); dfs(root); solve(); return 0; }
int main(int argc , char *argv[]) { int n, k, x, y, z; int c = 0; int px, py, vx, vy; for (int i = 0 ; i < Max ; i++) fa[i] = i; memset(val, sizeof(val), 0); scanf("%d%d", &n, &k); for (int i = 1 ; i <= k ; i++) { scanf("%d%d%d", &z, &x, &y); if (x > n || y > n ) { c ++ ; continue ; } px = findroot(x); py = findroot(y); vx = val[x]; vy = val[y]; if (px == py) if (z == 1) if (vy == vx) continue; else c ++ ; else if ((vx - vy + N ) % N == 1) continue; else c ++ ; else if (z == 1) Union_set(y, x, 0); else Union_set(y, x, 1); } printf("%d\n", c); return 1; }
void work(int x) { v[0] = now = x + 1; for (int k = h[x]; ~k; k = nxt[k]) if (mk[k]) { d[p[k]] = w[k]; e[p[k]] = 1; dfs1(p[k], x); dfs2(p[k], x); } findroot(x, n); for (int k = h[x]; ~k; k = nxt[k]) if (mk[k]) { mk[k ^ 1] = false; f[n] = size = s[p[k]]; findroot(p[k], cur = n); work(cur); } }
TreeNode *build(vector<int> &inorder, int i1, int i2, vector<int> &postorder, int p1, int p2) { if (p1 >= p2 || i1 >= i2) return nullptr; int v = postorder[p2-1]; int i = findroot(inorder, i1, i2, v); TreeNode *root = new TreeNode(v); root->left = build(inorder, i1, i, postorder, p1, p1+i-i1); root->right = build(inorder, i+1, i2, postorder, p1+i-i1, p2-1); return root; }
void cpu_rootconf(void) { findroot(); printf("boot device: %s\n", booted_device ? booted_device->dv_xname : "<unknown>"); setroot(booted_device, booted_partition); }
void cpu_rootconf(void) { findroot(); printf("boot device: %s\n", booted_device ? device_xname(booted_device) : "<unknown>"); rootconf(); }
void cpu_rootconf(void) { findroot(); matchbiosdisks(); aprint_normal("boot device: %s\n", booted_device ? device_xname(booted_device) : "<unknown>"); rootconf(); }
void findroot(int x, int pre) { s[x] = 1; f[x] = 0; for (int k = h[x]; ~k; k = nxt[k]) if (mk[k] && p[k] != pre) { findroot(p[k], x); s[x] += s[p[k]]; checkmax(f[x], s[p[k]]); } checkmax(f[x], size - s[x]); if (f[x] < f[cur]) cur = x; }
int main() { long long int n,m,u,v,k,w,sum=0,nfs[100001],r1,r2,total=0; printf("Number of vertices and edges: "); scanf("%lld%lld",&n,&m); printf("Endpoints u v & weight w for M edges\n"); for(k=0;k<m;k++) { printf("u v w: "); scanf("%lld%lld%lld",&u,&v,&w); edg[k][0]=w; edg[k][1]=u; edg[k][2]=v; total+=w; } qsort(edg,m,3*sizeof(edg[0][0]),fun); //printf("**%lld%lld**",edg[0][2],edg[1][2]); //Nifty Storage Truck for(k=1;k<=n;k++) nfs[k]=-1; printf("Edges in MST\n"); for(k=0;k<m;k++) { r1=findroot(nfs,edg[k][1]); r2=findroot(nfs,edg[k][2]); if(r1==r2) { continue; } else { printf("%lld %lld\n",edg[k][1],edg[k][2]); sum+=edg[k][0]; disunion(nfs,r1,r2); } } printf("Weight of MST: %lld\n",sum); return 0; }
long long int findroot(long long int nfs[],long long int a) { long long int t; if(nfs[a]<0) { return a; } else { t=findroot(nfs,nfs[a]); nfs[a]=t; return t; } }
int findroot(int x) { int fx = fa[x]; if (fx == x) { return x; } else { fa[x] = findroot(fa[x]); val[x] = (val[fx] + val[x]) % N; return fa[x]; } }
void cpu_rootconf() { struct device *booted_device; int booted_partition; findroot(&booted_device, &booted_partition); matchbiosdisks(); printf("boot device: %s\n", booted_device ? booted_device->dv_xname : "<unknown>"); setroot(booted_device, booted_partition, i386_nam2blk); }
int main() { scanf("%d%d", &n, &m); memset(h, -1, sizeof h); for (int i = 1; i < n; ++i) { int x, y, z; scanf("%d%d%d", &x, &y, &z); addedge(x, y, z); addedge(y, x, z); } ans = f[n] = size = n; findroot(0, cur = n); work(cur); printf("%d\n", ans < n ? ans : -1); return 0; }
void cpu_rootconf() { struct device *booted_device; int booted_partition; findroot(&booted_device, &booted_partition); #ifdef DEBUG_KERNEL_START printf("survived findroot()\n"); #endif setroot(booted_device, booted_partition, amiga_nam2blk); #ifdef DEBUG_KERNEL_START printf("survived setroot()\n"); #endif }
void cpu_rootconf(void) { findroot(); matchbiosdisks(); if (booted_wedge) { KASSERT(booted_device != NULL); aprint_normal("boot device: %s (%s)\n", device_xname(booted_wedge), device_xname(booted_device)); setroot(booted_wedge, 0); } else { aprint_normal("boot device: %s\n", booted_device ? device_xname(booted_device) : "<unknown>"); setroot(booted_device, booted_partition); } }
int main(void){ int m, n; for(RI(n), RI(m); (m || n); RI(n), RI(m)){ int a, ans = 0, b, maxppa = MINPPA, ppa; FOR(i, n) cnt[i] = 0, p[i] = i; FOR(i, n) FORI(j, i + 1, n) adj[i][j] = MINPPA; FOR(i, m){ RI(a), RI(b), RI(ppa); --a; --b; if(a > b) swap(a, b); if(ppa > adj[a][b]){ adj[a][b] = ppa; maxppa = max(maxppa, ppa); } } FOR(i, n) FORI(j, i + 1, n) if(adj[i][j] == maxppa) unionfind(i, j); FOR(i, n) ans = max(ans, ++cnt[findroot(i)]); printf("%d\n", ans); }
static void prom_get_device_args(const char *prop, char *args, unsigned int sz) { const char *cp; char buffer[128]; cp = prom_getpropstringA(findroot(), prop, buffer, sizeof buffer); /* * Extract device-specific arguments from a PROM device path (if any) */ cp = buffer + strlen(buffer); while (cp >= buffer) { if (*cp == ':') { strncpy(args, cp+1, sz); break; } cp--; } }
int prtc_gettime(todr_chip_handle_t handle, struct timeval *tv) { u_int32_t tod = 0; char buf[32]; if (OF_getprop(findroot(), "name", buf, sizeof(buf)) > 0 && strcmp(buf, "SUNW,SPARC-Enterprise") == 0) { tv->tv_sec = prom_opl_get_tod(); tv->tv_usec = 0; return (0); } snprintf(buf, sizeof(buf), "h# %08lx unix-gettod", (long)&tod); OF_interpret(buf, 0); tv->tv_sec = tod; tv->tv_usec = 0; return (0); }
void findgraph( long double a, long double b, long double i, long double (*f)(long double), char * name){ long double previous = f(a); long double current = f(a); unsigned long count = 0; printf("Starting %s\n", name); for (; a<b; a+=i){ current = f(a); if (matchSign( previous, current)){ } else { printf("Found sign change from %Lf to %Lf\n", a-i, a); findroot (a-i, a, 10, f, name); } previous = current; count++; } printf("Done %s after %lu iterations\n\n", name, count); }
static void get_ncpus(void) { #ifdef MULTIPROCESSOR int node; char sbuf[32]; node = findroot(); sparc_ncpus = 0; for (node = OF_child(node); node; node = OF_peer(node)) { if (OF_getprop(node, "device_type", sbuf, sizeof(sbuf)) <= 0) continue; if (strcmp(sbuf, "cpu") != 0) continue; sparc_ncpus++; } #else sparc_ncpus = 1; #endif }
void cpu_rootconf(void) { findroot(); #if defined(MEMORY_DISK_HOOKS) /* * XXX * quick hacks for atari's traditional "auto-load from floppy on open" * installation md(4) ramdisk. * See sys/arch/atari/dev/md_root.c for details. */ #define RAMD_NDEV 3 /* XXX */ if ((boothowto & RB_ASKNAME) != 0) { int md_major, i; cfdata_t cf; struct md_softc *sc; md_major = devsw_name2blk("md", NULL, 0); if (md_major >= 0) { for (i = 0; i < RAMD_NDEV; i++) { cf = malloc(sizeof(*cf), M_DEVBUF, M_ZERO|M_WAITOK); if (cf == NULL) break; /* XXX */ cf->cf_name = md_cd.cd_name; cf->cf_atname = md_cd.cd_name; cf->cf_unit = i; cf->cf_fstate = FSTATE_STAR; /* XXX mutex */ sc = device_private(config_attach_pseudo(cf)); if (sc == NULL) break; /* XXX */ } } } #endif rootconf(); }
/* * Hunt through the device tree for CPUs. There should be no need to * go more than four levels deep; an UltraSPARC-IV on Seregeti shows * up as /ssm@0,0/cmp@0,0/cpu@0 and a SPARC64-VI will show up as * /cmp@0,0/core@0/cpu@0. */ int get_ncpus(void) { #ifdef MULTIPROCESSOR int node, child, stack[4], depth, ncpus; char buf[32]; stack[0] = findroot(); depth = 0; ncpus = 0; for (;;) { node = stack[depth]; if (node == 0 || node == -1) { if (--depth < 0) return (ncpus); stack[depth] = OF_peer(stack[depth]); continue; } if (OF_getprop(node, "device_type", buf, sizeof(buf)) > 0 && strcmp(buf, "cpu") == 0) ncpus++; child = OF_child(node); if (child != 0 && child != -1 && depth < 3) stack[++depth] = child; else stack[depth] = OF_peer(stack[depth]); } return (0); #else return (1); #endif }