Example #1
0
void convex_hull(vector<Point> & a) {
	if (a.size() == 1)
		return;
	sort(a.begin(), a.end(), &cmp);
	Point p1 = a[0], p2 = a.back();
	vector<Point> up, down;
	up1.clear();
	down1.clear();
	up.push_back(p1); up1.pb(0);
	down.push_back(p1); down1.pb(0);
	for (size_t i = 1; i < a.size(); ++i) {
		if (i == a.size() - 1 || ccw(p1, a[i], p2)) {
			while (up.size() >= 2 && !ccw(up[up.size() - 2], up[up.size() - 1], a[i]))
				up.pop_back(),up1.pop_back();
			up.push_back(a[i]),up1.push_back(i);
		}
		if (i == a.size() - 1 || cw(p1, a[i], p2)) {
			while (down.size() >= 2 && !cw(down[down.size() - 2], down[down.size() - 1], a[i]))
				down.pop_back(),down1.pop_back();
			down.push_back(a[i]),down1.push_back(i);
		}
	}
	if (up[0].x == up.back().x && up[0].y == up.back().y)
		up.erase(up.end() - 1),up1.erase(up1.end()-1);
	a.clear();a1.clear();	
	for (size_t i = 0; i < up.size(); ++i)
		a.push_back(up[i]),a1.push_back(up1[i]);
	for (size_t i = down.size() - 2; i > 0; --i)
		a.push_back(down[i]),a1.push_back(down1[i]);
}
Example #2
0
int main(){
    int t, n, i, c;
    scanf("%d", &t);
    kase=1;
    while(t--){
        scanf("%s", str);
        n = strlen(str);
        grps.clear();
        type.clear();
        for(i=0; i<n; ){
            if(str[i]=='a'){
                c=0;
                while(i<n && str[i]=='a') c++, i++;
                grps.pb(c);
                type.pb(0);
            }else{
                c=0;
                while(i<n && str[i]=='b') c++, i++;
                grps.pb(c);
                type.pb(1);
            }
        }

        done = (1<<grps.size())-1;
        printf("%d\n", recur(0));
        kase++;
    }
    return 0;
}
int main() {
    int tc;
    cin >> tc;
    while (tc--) {
        int n, m;
        cin >> n >> m;
        AdjList.clear(); AdjList.resize(n);
        for (int i = 0; i < m; i++) {
            int a, b;
            cin >> a >> b;
            AdjList[a].push_back(ii(b, 0));
        }
        bool cac = true;
        int numScc = 0;
        dfs_num.clear(); dfs_num.resize(n, UNVISITED);
        dfs_parent.clear(); dfs_parent.resize(n, 0);
        for(int i = 0; i < n; i++) {
            if(dfs_num[i] == UNVISITED) {
                numScc++;
                if(graphCheck(i)) {
                    cac = false;
                    break;
                }
            }
        }
        if(cac && numScc == 1) cout << "YES\n";
        else cout << "NO\n";
    }
    return 0;
}
int main(){
  int N, M, a, b, c;
  while(cin >> N >> M && N != 0 && M != 0){
    AdjList.resize(N);
    dfs_num.resize(N);
    visited.resize(N);
    dfs_low.resize(N);
    numSCC = dfsNumberCounter = 0;

    for(int i = 0; i < M; i++){
      cin >> a >> b >> c;
      AdjList[a-1].push_back(b-1);
      if(c == 2) AdjList[b-1].push_back(a-1);
    }

    for(int i = 0; i < N; i++)
      if(dfs_num[i] == 0)
	tarjan(i);

    if(numSCC == 1) cout << 1 << endl;
    else cout << 0 << endl;

    visited.clear();
    AdjList.clear();
    dfs_low.clear();
    dfs_num.clear();
    S.clear();
  }
}
Example #5
0
int main()
{
    p.clear(), sz.clear();
    p.assign(33333, 0), sz.assign(33333, 0);
    while(1)
    {
        scanf("%d %d", &n, &m);
        if(!n && !m)
            break;
        initSet(n+1);
        num=1;
        while(n--)
        {
            scanf("%s", a);
            mp[a]=num++;
        }
        while(m--)
        {
            scanf("%s %s", a, b);
            Union(mp[a], mp[b]);
        }
        printf("%d\n", ans);
    }
    return 0;
}
int main() {
	#ifndef ONLINE_JUDGE
	freopen("test.inp", "r", stdin);
	//freopen("test.out", "w", stdout);
	#endif
	int cases, caseNo = 0;
	for (scanf("%d", &cases); cases--; ) {
		scanf("%d %d", &n, &k);
		rep(i, n) scanf("%d", &a[i]);
		set1.clear(); set2.clear();
		buildSet(n / 2 + 1, 0, 0, set1); buildSet(n, n / 2 + 1, 0, set2);
		sort(all(set2));
		bool ok = false;
		for (int i = 0; i < set1.size() && !ok; i++) {
			int lo = 0, hi = set2.size() - 1, val = k - set1[i];
			while (!ok && lo <= hi) {
				int mid = (lo + hi) / 2;
				if (set2[mid] == val)
					ok = true;
				else if (val > set2[mid])
					lo = mid + 1;
				else hi = mid - 1;
			}
		}
		printf("Case %d: %s\n", ++caseNo, ok ? "Yes" : "No");
	}
	return 0;
}
int main () {
    #ifndef ONLINE_JUDGE
    freopen(INP, "r", stdin); freopen(OUT, "w", stdout);
    #endif
    
    scanf("%d", &n);
    order.clear();
    arr1.assign(n+1,0);
    a1.clear();
    FOR(i,1,n) scanf("%d", &a), a1.pb(a);
    REP(i,n) arr1[a1[i]-1] = i+1;
    REP(i,n) order[arr1[i]] = i;
    while(scanf("%d", &a) != EOF){
        arr.assign(n+1,0);
        a2.clear();
        a2.pb(a);
        ans.resize(0);
        len = 0;
        FOR(i,1,n-1) scanf("%d", &a), a2.pb(a);
        REP(i,n) arr[a2[i]-1] = i+1;
        REP(i,n) updateLis(arr[i]);   
        cout << len << endl; 
    }

    return 0;
}
int main(){
	// Test
	int d, Case =1;
	while(scanf("%d", &n) && n){
		t.clear();s.clear();
		for (int i = 1; i <= n; ++i)
			s.push_back(i);

		for (int i = 0, tmp; i < n; ++i){
			scanf("%d", &tmp);
			t.push_back(tmp);
		}

		printf("Case %d: ",Case++);

		if((d = bfs(t,s,2,dist_t,dist_s)) != INF){
			printf("%d\n", d);
		}else if((d = bfs(s,t,2,dist_s,dist_t)) != INF){
			printf("%d\n", d);
		}else
			printf("5\n");

	}

	return 0;
}
int main()
{
	int temp,i;
	char c;
	cin>>n;
	perm.pb(0);
	done.pb(0);
	for( i=1;i<=n;i++)
	{
		cin>>temp;
		perm.pb(temp);
		done.pb(0);
	}
	
	for( i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			cin>>c;
			if(c=='1')
		    mat[i][j]=1;
		    else
		    mat[i][j]=0;
		}
	}
	
	for(i=1;i<n;i++)
	{
		if(done[i]==0)
		{
		   bfs(i);
		   
		   
		   for(int j=0;j<help.size();j++)
		   sorted.pb(perm[help[j]]);
		   
		   
		   sort(help.begin(),help.end());
		   sort(sorted.begin(),sorted.end());
		   
		   for(int j=0;j<help.size();j++)
		   {
		   	 perm[help[j]]=sorted[j];
		   }
		   
		   help.clear();
		   sorted.clear();
		}
    }
    
    for(i=1;i<n;i++)
    cout<<perm[i]<<" ";
    cout<<perm[i];
    
    return 0;
}
Example #10
0
void buildUF(int n){
    p.clear();
    p.assign(n+1,0);
    rnk.clear();
    for (int i = 0; i <= n; i++) {
        p[i]=i;
        //members.push_back(1);
        rnk.push_back(1);
    }

}
Example #11
0
bool TopologicalSort(const Graph& g,vi& order)
{
	int size=g.size();
	order.clear();
	vi colors(size);
	rep(i,size){
		if(!Visit(g,i,colors,order)){
			order.clear();
			return false;
		}
	}
	reverse(all(order));
	return true;
}
Example #12
0
int main(){
	FASTER;
	n = 12;

	while(1){
		values.clear();
		bool end = false;
		for (int i = 0,t; i < 12; ++i) {
			cin >> t;
			values.push_back(t);
			end = end || t>0;
		}
		if(!end)break;

		permu.assign(12, 0);
		vis.assign(12, 0);

		vis[0] = 1;
		permu[0] = values[0];
		cnt = 0;

		go(0);

		permu.assign(12, 0);
		permu[1] = values[0];

		go(0);

		cout << cnt/2 << endl;

	}

	return 0;
}
Example #13
0
int main() 
{
    int T;
    sf("%d", &T);
    while (T--)
    {
        sf("%d%d%d", &n, &s, &w);
        int g = s;
        REP(i, n)
        {
            a[i] = g;
            if (a[i] == 0) a[i] = g = w;
            if (g%2 == 0) g /= 2;
            else g = (g/2) ^ w;
        }
        //REP(i, n) cout <<a[i] <<" ";
        //cout <<endl;
        st = 0;
        //REP(i, n) st ^= a[i];
        b.clear();
        b.pb(0);
        REP(i, n)
        {
            st ^= a[i];
            b.pb(st);
        }
Example #14
0
int main() {
    int n, m;
    int caso = 0;
    while(1) {
        edges.clear();
        used.clear();
        scanf(" %d %d", &n, &m);
        for (int i = 1 ; i <= n; i++) maximum_tree[i].clear();
        if (!n && !m) break;
        used.assign(n+1, 0);
        for (int i=0 ; i < m; i++) {
            int a, b, c;
            scanf(" %d %d %d", &a, &b, &c);
            edges.pb(mp(c-1, mp(a,b)));
        }
        Kruskal(n);

        // for (int i = 1; i <= n; i++) {
        //     cout << i << "-> ";
        //     for (int j = 0; j < (int) maximum_tree[i].size(); j++) {
        //         cout << "(" << maximum_tree[i][j].fi << ", " << maximum_tree[i][j].sc << ") ";
        //     }
        //     cout << endl;
        // }

        int from, p;
        scanf(" %d %d %d", &from, &go, &p);
        double ans = ceil((double) p / (double) dfs(from));
        cout << "Scenario #" << ++caso << endl;
        cout << "Minimum Number of Trips = " << ans << endl<< endl;
    }
}
int main() {
    //READ("in.txt");
    //WRITE("out.txt");

    int t;
    getI(t);
    for(int ci=1;ci<=t;ci++)
    {
        v.clear();
        SET(dp);
        CLR(pri);
        getI(totLen);
        totLen*=100;
        len=0;
        int x;
        while(getI(x) && x)
        {
            //x/=100;
            v.PB(x);
            len++;
        }
        cout << call(0,0,0) <<endl;
        print(0,0,0);
        if(ci<t) printf("\n");
    }
    return 0;
}
int main()
{
	vi dist;
	int tested;
	scanf("%d", &tested);
	while(tested--)
	{
		lista.assign(MAX*MAX, vi());
		dist.assign(MAX*MAX, -1);
		toposort.clear();
		string name;
		cin>>name>>row>>column;
		int n=0;
		for(int i=0;i<row;i++)
			for(int j=0;j<column;j++)
				scanf("%d", &a[i][j]), b[i][j]=n++;
		generate(n);
		int ans=-1;
		for(int i=0;i<toposort.size();i++)//MAX path
		{
			int u=toposort[i];
			ans=min(ans , dist[u]);
			for(int j=0;j<lista[u].size();j++)
			{
				int v=lista[u][j];
				dist[v]=min(dist[v], dist[u] - 1); // update distance
			}
		}
		cout<<name<<": "<<-ans<<endl;
	}
}
Example #17
0
int main() {
    #ifdef dipta007
        READ("in.txt");
        //WRITE("out.txt");
    #endif // dipta007

    int k;
    while(~getII(n,k))
    {
        v.clear();
        res.clear();
        FOR(i,0,n-1) getI(a[i]);
        SET(dp);
        int kk = call1(0,k);

        CLR(vis);
        tc=0;
        print(0,k);

        printf("%d\n",res.size());
        set<int> :: iterator i;
        for(i=res.begin();i!=res.end();i++)
        {
            if(i!=res.begin()) printf(" ");
            printf("%d",*i);
        }
        printf("\n");
    }

    return 0;
}
Example #18
0
int TheArray::find (int _n, int _d, int _first, int _last) 
{
	arr.clear();
	d = abs(_d);
	n = _n;
	first = _first;
	last = _last;
	if(first > last)
		swap(first, last);

	arr.pb(first);

	while(arr.size() < n){
		lft = n - arr.size();
		if(arr.back() + 0 - d*(lft-1) <= last)
			arr.pb(arr.back() + find1(0, d));
		else
			arr.pb(max(arr.back()-d, last));
	}
/*
	tr(it, arr)
		write(*it);
	cout << endl;
*/

	return *max_element(all(arr));
}
Example #19
0
int main() {
	scanf("%d %d", &n, &m);
	while( m-- ) {
		scanf("%d %d", &v, &k);
		while( k-- ) 
			scanf("%d", &u), g[u][v] = 1, deg[v]++;
	}
	ans.clear();
	while( (int)ans.size() != n ) {
		for( i = 1; i <= n; ++i )
			if( !deg[i] && !vis[i] ) {
				ans.push_back(i);
				vis[i] = 1;
				for( v = 1; v <= n; ++v ){
					if( g[i][v] )
						g[i][v] = 0, --deg[v];
				}
				break;
			}
	}
	for( i = 0; i < (int) ans.size(); ++i ) {
		if( i > 0 )
			putchar(' ');
		printf("%d", ans[i]);
	}
	putchar('\n');
	return 0;
}
Example #20
0
int findSolution() {
    p = vi(n, -1);

    int ans = 0;
    for (;;) {
        bool g = false;
        for (int i = 0; i < n; i++) if (ty[i] == 0 && p[i] < 0) {
                was = vb(n, false);
                chain.clear();
                if (dfs(i)) {
                    reverse(chain.begin(), chain.end());
                    assert(chain.size() % 2 == 0);
                    for (int i2 = 0; i2 < chain.size(); i2 += 2) {
                        p[chain[i2]] = chain[i2 + 1];
                        p[chain[i2 + 1]] = chain[i2];
                    }

                    g = true;
                    ans++;
                    break;
                }
            }
        if (!g) break;
    }
    return ans;
}
Example #21
0
int main(){
    int n, m;
    while(true){
        cin >> n >> m;
        if(n == 0 && m ==0) break;
        adj.assign(n,vi());
        visitados.assign(n,0);
        s.clear();
        for(int k = 0; k < m; k++){
            int principal, dependente;
            cin >> principal >> dependente;
            (adj.at(principal-1)).push_back(dependente-1);
        }
        for(int l = 0; l < n; l++){
            if(visitados[l] == 0){
                dfs(l);
            }
        }
        string result;
        for(int d = s.size()- 1 ; d > 0;d--){
            stringstream ss;
            ss << s.at(d);
            result += ss.str() + " ";
        }
        stringstream ss;
        ss << s.at(0);
        result += ss.str();
        cout << result << endl;
    }
}
Example #22
0
int main()
{
	v.assign(101, vector<ii>());
	visited.assign(101, 0);
	while(true)
	{
		topo.clear();
		for(i=0;i<101;++i)
			visited[i]=0;
		scanf("%d %d", &n, &m);
		if(!n && !m)
			break;
		for(i=1;i<=n;++i)
			v[i].clear();
		while(m--)
		{
			scanf("%d %d", &x, &y);
			v[x].push_back(make_pair(y, 1));
		}
		for(i=1;i<=n;++i)
		{
			if(!visited[i])
				dfs(i);
		}
		reverse(topo.begin(), topo.end());
		for(i=0;i<n;++i)
		{
			if(i>0)
				putchar(' ');
			printf("%d", topo[i]);
		}
		putchar('\n');
	}
	return 0;
}
int main(){
	findprimes();
	ll n, res;
	cin >> n;
	while(n!= 0){
		res = n;
		
		if(n < 0){
			results.push_back(-1);
			res = res/-1;
		}
		int idx = 0;
		while(res != 1){
			DEBUG(res);
			if(res%primos[idx] == 0){
				res = res/primos[idx];
				results.push_back(primos[idx]);
			}else{
				idx++;
			}
		}
		printf("%d = ", n);
		REP(i, results.size()-1) printf("%d x ", results[i]);
		printf("%d\n", results[results.size()-1]);
		
		
		cin >> n;
		results.clear();
	}
	
	
}
int main() {
    ios::sync_with_stdio(0);
    double d;
    int m, tc = 1;
    while( cin >> d >> m) {
        cout << "Case " << tc++ << ":" << endl;
        memo.clear(); memo.resize(105, -1);
        prices.clear(); prices.resize(m + 1);
        prices[0].first = 1; prices[0].second = (d + eps) * 100;
        for(int i = 1; i <= m; i++) {
            int tmp; cin >> tmp >> d;
            prices[i].first = tmp; prices[i].second = (d + eps) * 100;
        }
        string str;
        cin.get();
        getline(cin, str);
        stringstream ss(str);
        int tmp;
        while(ss >> tmp) {
            cout << "Buy " << tmp << " for $" << fixed << setprecision(2) << (double)solve(tmp) / 100 << endl;
        }
    }

    return 0;
}
int main(){
  ios_base::sync_with_stdio(false);
  string linha;
  //~ while(getline(cin,linha)){
    getline(cin, linha);
    //~ cout << linha << endl;
    memset(st, 0, sizeof st);
    cin >> m;
    ans.assign(m, 0);
    beg.assign(m, 0);
    qs.assign(linha.size(), vi());
    int a, b;
    for(int i = 0; i < m; i++){
      cin >> a >> b;
      beg[i] = a-1;
      qs[b-1].push_back(i);
    }
    op.clear();
    int q;
    for(int i = 0; i < (int)linha.size(); i++){
      if(linha[i] == &#39;)&#39;){
        if(op.size() > 0){
          update(1,0,(int)linha.size()-1, op.back());
          op.pop_back();
        }
      }
      for(int j = 0; j < (int)qs[i].size(); j++){
        q = qs[i][j];
        ans[q] = query(1, 0, (int)linha.size()-1, beg[q], i);
      }
      
      if(linha[i] == &#39;(&#39;) op.push_back(i);
    }
Example #26
0
int main() {
    int m, n;
    while(1) {
        edges.clear();
        ans.clear();
        scanf(" %d %d", &n, &m);
        if (!n && !m) break;
        int tot = 0;
        for (int i = 0; i < m; i++) {
            int a, b, c;
            scanf(" %d %d %d", &a, &b, &c);
            tot += c;
            edges.pb(mp(c, mp(a,b)));
        }
        int mn = Kruskal(n);
        if (tot == mn) {
            printf("forest\n");
        } else {
            printf("%d", ans[0]);
            for (int i = 1; i < (int) ans.size(); i++) {
                //if (!used[i]) {
                    printf(" %d", ans[i]);
                //}
            }
            printf("\n");
        }
        //cout << tot << " " << mn << endl;
    }
}
Example #27
0
 void clear()
 {
     dfs_num.clear();
     dfs_num.resize(v+1);
     connectedComp.clear();
     cnt = 0;
 }
Example #28
0
int main()
{
    path.clear(), topo.clear(), adj.clear(), vis.clear();
    adj.assign(111111, vector<ii>()), path.assign(111111, 0), vis.assign(111111, 0);
    while(scanf("%d", &n)!=EOF)
    {
        for(i=1;i<111111;++i)
            adj[i].clear();
        death.clear();
        for(i=1;i<=n;++i)
        {
            scanf("%d", &c);
            if(!c)
                death.push_back(i);
            while(c--)
            {
                scanf("%d", &j);
                adj[i].push_back(ii(j+1, 1));
            }
        }
        for(i=1;i<100000;++i)
            path[i]=0, vis[i]=0;
        topo.clear();
        for(i=1;i<=n;++i)
            if(!vis[i])
                dfs(i);
        reverse(topo.begin(), topo.end());
        path[1]=1;
        for(i=0;i<(int)topo.size();++i)
        {
            int u=topo[i];
            for(j=0;j<(int)adj[u].size();++j)
            {
                ii v=adj[u][j];
                path[v.first]+=path[u];
            }
        }
        if(blank)
            putchar('\n');
        blank=true;
        ans=0;
        for(i=0;i<(int)death.size();++i)
            ans+=path[death[i]];
        printf("%d\n", ans);
    }
    return 0;
}
int main() {
    int N;
    while (true) {
        path.clear();
        g.clear();
        circles.clear();
        num.clear();
        dicc.clear();
        cin >> N;
        if (N == 0) break;
        for (int i = 0; i < N; i++) {
            double x, y, r;
            cin >> x >> y >> r;
            circles.push_back(circle(point(x, y), r));
        }

        g.assign(N, vi());
        for (int i = 0; i < N; i++) {
            for (int j = i+1; j < N; j++) {
                circle c1 = circles[i], c2 = circles[j];
                if (dist(c1.c, c2.c) < c1.r + c2.r) {
                    g[i].push_back(j);
                    g[j].push_back(i);
                }
            }
        }

        C = 0;
        num.assign(N, 0);
        for (int i = 0; i < N; i++) {
            cycleCheck(i, -1);
            path.clear();
            num.assign(N, 0);
        }

        for (int i = 0; i < cycles.size(); i++) {
            printf("cycle %d: ", i);
            for (int j = 0; j < cycles[i].size(); j++) {
                printf("%d ", cycles[i][j]);
            }
            printf("\n");
        }
    }

    return 0;
}
int maxFlow() {
    int result = 0;
    while(dinic_bfs()) {
        work.clear(); work.resize(n, 0);
        while(int d = dinic_dfs(s, inf)) result += d;
    }
    return result;
}