Beispiel #1
0
bool ok(int l){

	int c1 = 0;
	int c2 = 0;

	for (int i = 1; i <= l; ++i) {
		if(a[i] < a[c1])c1 = i;
		if(b[i] < b[c2])c2 = i;
	}
	vector<pair<ll, ii> > p;
	for (int i = 0; i < m; ++i) {
		if(tp[i]==1) p.push_back(pair<ll, ii> (a[c1]*c[i], ii(i,c1)));
		if(tp[i]==2) p.push_back(pair<ll, ii> (b[c2]*c[i], ii(i,c2)));
	}
	sort(p.begin(), p.end());
	int sum = 0;
	int cnt = 0;
	X.clear();
	for (int i = 0; i < p.size(); ++i) {
		if(sum+p[i].first>s)break;
		sum+=p[i].first;
		X.push_back(p[i].second);
		cnt++;
	}
	return cnt >= k;
}
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;
}
Beispiel #3
0
int goto_free_point(const vii& g, int& t){
  vi bfs;
  vi res;
  vi revisited;
  revisited.resize(num_intersect);

  for(int i=0; i < num_intersect; ++i)
    revisited[i] = 0;

  for(int i=0; i < (int)g.size(); ++i)
    bfs.push_back(g[i].first);
  int go;
  while(!bfs.empty()){
    go = bfs.back();
    bfs.pop_back();
    if(visited[go] && !revisited[go]){
      res.push_back(go);
      revisited[go] = 1;
      for(int i=0; i < (int)g.size(); ++i)
        if(!revisited[go])
          bfs.push_back(graph[go][i].first);
    }
    else{
      break;
    }
  }
        for(int i=0; i < (int)res.size(); ++i)
        vpath[veh].push_back(res[i]);
      return go;
}
Beispiel #4
0
int main(){
    int vertices,arestas,caso=1;
    while(scanf("%d %d", &vertices, &arestas) && vertices && arestas){
        for(int i = 0; i < arestas; i++)
        {
            int u,v;
            scanf("%d %d", &u, &v);
            adj[u].push_back(v);
            adj[v].push_back(u);
        }
        memset(dfs_num, -1 , sizeof(dfs_num));
        memset(visitados, 0 , sizeof(visitados));
        dfsNumbercounter = 0;

        for(int i = 0; i <= vertices; i++){
            if(dfs_num[i]==DFS_WHITE){findBridges(i);}
        }

        cout << caso++ << endl;
        cout << endl;

        for(int i = 0; i < bridge.size();i++){
            printf("%d %d\n",bridge[i].first,bridge[i].second);
        }
        cout << "#" << endl;
        bridge.clear();
        int i=vertices+5;
        while(i--){
            adj[i].clear();
        }
    }
    return 0;
}
int main(){
	FASTER;
	int n;
	int Case=1;
	while(cin >> n, n){
		cas.clear();
		for(int i = 0 ; i < n ; i++){
			int a,m,g;
			cin >> a >> m >> g;
			castle c{a,m,g};
			C.push_back(c);
			cas.push_back(ii(max(a,m+g), m+g));
		}
		adj.assign(n,vi());
		for(int i = 0 ; i < n-1;i++){
			int u,v;cin >> u >> v;
			u--,v--;
			adj[u].push_back(v);
			adj[v].push_back(u);
		}	
		ii ans = ii(1e9,1e9);
		
		for(int i = 0 ; i < 1;i++){
			ans = min(ans,dfs(i));
		}
		cout << "Case " << (Case++)<< ": ";
		cout << ans.first << endl;
	}
}
int main()
{
	ifstream fin("marathon.in");
	ofstream fout("marathon.out");

	int N; 
	fin >> N;
	points.resize(N);
	for (int i = 0; i < N; ++i)
	{
		fin >> points[i].first >> points[i].second;
	}

	T.resize(N - 2);
	res.resize(N - 2);

	T[0].first = 0;
	T[0].second =  dist_cont(2, N-1);
	res[0] = T[0].first + T[0].second + dist_two(0, 2);
	int result = res[0];

	for (int k = 1; k <= N - 3; ++k)
	{
		T[k].first = T[k - 1].first + dist_two(k - 1, k);
		T[k].second = T[k - 1].second - dist_two(k + 1, k+2);
		res[k] = T[k].first + T[k].second + dist_two(k, k + 2);
		result = min(result, res[k]);
	}
	fout << result << '\n';
	
	return 0;
}
void doGreedy()
 {
 	int cek;
 	LL total = 0;
 	// SOrt dlu
 	sort(dragons.begin(), dragons.end());
 	sort(knights.begin(), knights.end());
 	
 	REP(a,n)
 	{
 		cek = 0;
 		REP(b,m)
 		{
 			//printf("dragon : %d \n",dragons[a]);
 			
 			// Ga bisa dipake lagi
 			if(knights[b].second == 1) continue;
 			
 			if(knights[b].first >= dragons[a] )
 			{
 				//printf("KILL by : %d\n",knights[b].first);
 				cek = 1;
 				total += knights[b].first;
 				knights[b].second = 1;
 				break;
 			}
 		}
 		if(!cek)
 		{
 			break;
 		}
 	}
Beispiel #8
0
int main() {
    freopen("milk.in", "r", stdin);
    freopen("milk.out", "w", stdout);

    int n, m, i,j, a,b;
    long long p;

    scanf("%d %d", &n, &m);
    for(i=0; i<m; i++) scanf("%d %d", &a, &b), farmers.push_back(ii(a,b));

    sort(farmers.begin(), farmers.end());

    p=0;
    for(i=0; n>0; i++){
        if(n>=farmers[i].second){
            n-=farmers[i].second;
            p+=farmers[i].first*farmers[i].second;
        }else{
            p+=farmers[i].first*n;
            n=0;
        }
    }
    printf("%lld\n", p);
    return 0;
}
int main(){
    FILE *f;
    f =fopen("input/OrderingTasks.in", "r");
    
    #ifdef ONLINE_JUDGE
    f = stdin;
    #endif
    
    int m,n;
    while(fscanf(f, "%d %d", &n, &m) == 2 && n){
        g.clear();
        g.assign(n, vi());
        for(int i = 0 ; i < m ; i++){
            int u,v;
            fscanf(f, "%d %d", &u,&v);
            g[u-1].push_back(v-1);
        }
        vi sortedV;
        for(int i = 0 ; i < n ; i++){
            if(!visited[i])
                dfs(i, sortedV);
        }
        memset(visited, 0, sizeof visited);
        for(int i = sortedV.size() -1 ; i >= 0 ; i--){
            printf("%d", sortedV[i] + 1);
            if(i)printf(" ");
            else printf("\n");
        }
    }
    
    return 0;
}
int main() {
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  scanf("%d", &n);
  ll sum = 0;
  for(int i = 0; i < n; ++i) {
    int a, b; scanf("%d %d", &a, &b);
    sum += a;
    s.pb(mp(a, -1));
    s.pb(mp(b, 1));
  }
  sort(s.begin(), s.end());
  ll qtd = 0;
  ll maxi = sum;
  ll p = 0;
  for(int i = 0; i < s.size(); ++i) {
    if(s[i].S == -1) {
      qtd -= s[i].S;
      sum -= s[i].F;
    }
    ll foo = qtd * (ll)s[i].F + sum;
    if(foo > maxi) {
      maxi = foo;
      p = s[i].F;
    }
    if(s[i].S == 1) qtd -= s[i].S;
  }
  printf("%lld %lld\n", p, maxi);
  return 0;
}
Beispiel #11
0
void  findBridges(int u)
{
    dfs_num[u]=dfs_low[u]=dfsNumbercounter++;
    for(int i =0; i < adj[u].size();i++){
        int vizinho = adj[u][i];
        if(dfs_num[vizinho] == DFS_WHITE)
        {
            dfs_parent[vizinho] = u;

            findBridges(vizinho);

            if(dfs_low[vizinho]>dfs_num[u]){
                bridge.push_back(ii(u,vizinho));
                bridge.push_back(ii(vizinho,u));
            }

            else {bridge.push_back(ii(u,vizinho));}

            visitados[u][vizinho]=visitados[vizinho][u]=true;
            dfs_low[u]=min(dfs_low[u],dfs_low[vizinho]);
        }
        else if(vizinho!=dfs_parent[u]){
            dfs_low[u] = min(dfs_low[u],dfs_num[vizinho]);
            if(!visitados[u][vizinho]){
                bridge.push_back(ii(u,vizinho));
                visitados[u][vizinho]=visitados[vizinho][u]=true;
            }
        }
    }
}
Beispiel #12
0
	void sol(){

		if(s2<s1){
			vi t=a;a=b;b=t;
			ll x=s1;s1=s2;s2=x;
			t=ida;ida=idb;idb=t;
			fliped=!fliped;
		}

		if(a.empty() || b.empty())return ;

		if(abs(s1-s2)==0){return ;}

		ll diff = abs(s2 - s1);
		ll Y = diff;
		int x,y;
		x=y=0;
	//	printf("diff = %lld\n",diff);

		for (int i = 0; i < a.size(); ++i) {

			vi::iterator it = lower_bound(b.begin(), b.end(), diff/2 + a[i]);
			int j = it-b.begin();
			if(j && j == b.size())j--;

//			printf("i,j=%d,%d\n",i,j);

			ll ns1 = s1 - a[i] + b[j];
			ll ns2 = s2 - b[j] + a[i];

	//		printf("debug a[%d] = %d b[%d] = %d\n",i,a[i],j,b[j]);
	//		printf("debug ns1 = %lld ns2 = %lld\n",ns1,ns2);
			if(abs(ns1 - ns2) < Y){
				x = i;
				y = j;
	//			printf("debug %d %d\n",x,y);
				Y = abs(ns1 - ns2);
			}
		}



		if(Y<diff){
//			printf("swaping a[%d] b[%d] diff = %lld\n", x,y, Y);
			if(fliped)
				ans.push_back(ii(idb[y],ida[x]));
			else
				ans.push_back(ii(ida[x],idb[y]));
			s1 = s1 - a[x] + b[y];
			s2 = s2 + a[x] - b[y];
		}

//		printf("x,y = %d %d\n",x,y);

		a.erase(move(a,x));
		ida.erase(move(ida,x));
		b.erase(move(b,y));
		idb.erase(move(idb,y));
	}
int main(){
	int tc, n, g, u, v;
	cin >> tc;
	while (tc--){
		cin >> n >> g;
		
		AdjList.assign(n, vi());
		for (int i = 0; i < g; ++i){
			cin >> u >> v;
			--u; --v;
			AdjList[u].push_back(v);
		}
		if (n == 0){
			cout << 0 << endl;
			continue;
		}
		dfs_num.assign(n, DFS_WHITE);
		dfs_low.assign(n, 0);
		visited.assign(n, 0);
		scc_weight.assign(n, 0);
		scc_id.assign(n, -1);
		dfsCounter = numSCC = 0;
		for (int i = 0; i < n; i++)
			if (dfs_num[i] == DFS_WHITE)
				tarjanSCC(i);
		
		SccAdj.assign(numSCC, vi());
		for (int u = 0; u < n; ++u)
			for (auto &v : AdjList[u])
				if (scc_id[v] != scc_id[u])
					SccAdj[scc_id[u]].push_back(scc_id[v]);
		

		dfs_num.assign(numSCC, DFS_WHITE);
		topoSort.clear();
		for (int i = 0; i < numSCC; i++)
			if (dfs_num[i] == DFS_WHITE)
				dfs(i);

		vi dist(numSCC, 0);
		int best = 1;
		for (int u = topoSort.size() - 1; u >= 0; --u){
			dist[topoSort[u]] = min(dist[topoSort[u]], scc_weight[topoSort[u]]);
			best = min(best, dist[topoSort[u]]);
			for (auto &v : SccAdj[topoSort[u]]){
				dist[v] = min(dist[v], dist[topoSort[u]] + scc_weight[v]);
				best = min(best, dist[v]);
				
			}
		}
		cout << -best << endl;
	}
}
Beispiel #14
0
void out(const vii & x, int b, int e) {
    if (b >= e || x[b].first == x[e - 1].first)
        return;
    int mid = x[(b + e) / 2].first;
    int i = b;
    for (; i < x.size() && x[i].first < mid; ++i)
        res.push_back(pii(mid, x[i].second));
    out(x, b, i);
    for (i = e - 1; i >= b && x[i].first > mid; --i)
        res.push_back(pii(mid, x[i].second));
    out(x, i + 1, e);
}
Beispiel #15
0
	int main(){
		FASTER;
		vii temp;
		cin >> n;
		s1=s2=0;
		for (int i = 0,t; i < n; ++i) {
			cin >> t;
			temp.push_back(ii(t,i));
		}
		sort(temp.begin(),temp.end());
		for (int i = 0; i <n; ++i) {
			a.push_back(temp[i].first);
			ida.push_back(temp[i].second+1);
		}
		temp.clear();
		cin >> m;
		for (int i = 0,t; i <m; ++i) {
			cin >> t;
			temp.push_back(ii(t,i));
		}
		sort(temp.begin(),temp.end());
		for (int i = 0; i < m; ++i) {
			b.push_back(temp[i].first);
			idb.push_back(temp[i].second+1);
		}
		temp.clear();

		s1=s2=0;
		for (int i = 0; i < a.size(); ++i) {
			s1+=a[i];
		}
		for (int i = 0; i < b.size(); ++i) {
			s2+=b[i];
		}



		sol();sol();
	//	printf("%lld %lld\n",s1,s2);


		cout << abs(s1-s2) << endl;

		cout  << ans.size() << endl;
		if(ans.size()){
			for (int i = 0; i < ans.size(); ++i) {
				cout << ans[i].first << " " << ans[i].second  << endl;
			}
		}

		return 0;
	}
void primeFactors(ll N, vii &factors) {
	ll PF_idx = 0, PF = primes[PF_idx];
	while (PF * PF <= N){
		ii f(PF, 0);
		while (N % PF == 0){
			N /= PF;
			f.second++;
		}
		if (f.second > 0){
			factors.push_back(f);
		}
		PF = primes[++PF_idx];
	}
	if (N != 1) factors.push_back(ii(N,1));
}
Beispiel #17
0
int get_road(int ridx, const vii& g, int &t){
  coord_t dir = directions[ridx];
  double min_dist = 800000000.0;
  int res = -1;
  for(int i = 0; i < (int)g.size(); ++i){
    double min_dist2= dist(dir, coords[g[i].first]);
    if(visited[g[i].first] < 2 && min_dist > min_dist2){
      min_dist = min_dist2;
      res = i;
    }
  }
  // if(res == -1) return goto_free_point(g, t);
  // return res;
  return (res == -1)?droad(rd)%g.size():res;
}
Beispiel #18
0
int main(){
    int t, n, i,j, x,y;
    scanf("%d", &t);
    while(t--){
        scanf("%d", &n);
        ps.clear();
        for(i=0; i<n; i++){ scanf("%d", &x); ps.push_back(ii(x,-1)); }
        for(i=0; i<n; i++){ scanf("%d", &x); ps[i].second=x; }

        sort(ps.begin(), ps.end());
        ys.clear();
        for(i=0; i<n; i++) ys.push_back(ps[i].second);
        printf("%d\n", LIS_Len(ys));
    }
    return 0;
}
int main()
{
	int test;
	scanf("%d", &test);
	for(int te=1;te<=test;te++)
	{
		grip.clear();
		data.clear();
		scanf("%d %d", &n, &m);
		for(int i=0;i<n;i++)
		{
			string x;
			cin>>x;
			grip.push_back(x);
		}
		getGraph();
		sol(piii(pii(data[0].first, data[0].second),0));
		int pos=-1;
		for(int i=0;i<4;i++)
			if(d[data[1].first][data[1].second][i] !=-1)
			{
				pos=i;
				break;
			}
		if(pos>=0)
			printf("Case #%d: %d\n", te,d[data[1].first][data[1].second][pos]);
		else
			printf("Case #%d: impossible\n", te);
	}
}
Beispiel #20
0
int main() {
    ios::sync_with_stdio(0);
    int tc; cin >> tc;
    for(int i = 1; i <= tc; i++) {
        cout << "Case #" << i << ": ";
        int m; cin >> n >> m >> k;
        graph.assign(n, vi(n, inf)), works.assign(k, ii(0, 0));
        for(int i = 0; i < n; i++) graph[i][i] = 0;
        for(int i = 0; i < m; i++) {
            ll a, b, c; cin >> a >> b >> c;
            a--, b--;
            graph[a][b] = graph[b][a] = min(graph[a][b], c);
        }
        for(int i = 0; i < k; i++) {
            int a, b;
            cin >> a >> b;
            works[i].first = a - 1, works[i].second = b - 1;
        }
        for (int k = 0; k < n; k++)
            for (int i = 0; i < n; i++)
                for (int j = 0; j < n; j++) graph[i][j] = min(graph[i][j], graph[i][k] + graph[k][j]);
        memo.assign(k + 1, vi(n + 1, -1));
        int ans = solve(0, works[0].first) + graph[0][works[0].first];
        if(ans >= inf) cout << -1 << endl;
        else cout << ans << endl;
    }


    return 0;
}
Beispiel #21
0
int main() {
    int n,x,c;
    scanf("%d", &n);

    for(int i=0; i<n; i++){
        scanf("%d %d", &x, &c);
        m.pb(ii(x,c));
    }
    sort(m.begin(), m.end());

    memset(dp, -1, sizeof dp);
    printf("%d\n", m[0].second+recur(1,0));

    return 0;

}
Beispiel #22
0
int main(){
    //maketestcase();
    //freopen("input.txt", "r", stdin);
    int n;
    while(scanf("%d", &n) != EOF){
        v.clear();
        for(int i = 0; i < n; i++){
            al[i].clear();
            visited[i] = false;
        }
        for(int i = 0; i < n; i++){
            int x, y;
            scanf("%d %d", &x, &y);
            v.pb(mkp(x,y));
        }

        map<ii, vi> m;
        for(int i = 0; i < n; i++){
            int x = v[i].first;
            int y = v[i].second;
            m[mkp(x/5, y/5)].pb(i);
        }
        int ix[5] = {0, 1, 1, 1, 0};
        int iy[5] = {1, 1, 0, -1, -1};
        repSTL(m, iter){
            generateGraph(iter->second,iter->second);
            ii p = iter->first;
            for(int i = 0; i < 5; i++){
                int x = p.first + ix[i];
                int y = p.second + iy[i];
                typeof(m.begin()) iter2 = m.find(mkp(x,y));
                if(iter2 != m.end()){
                    generateGraph(iter->second, iter2->second);
                }
            }
        }
        m.clear();

        int minS = 0;
        for(int i = 0; i < n; i++){
            if(!visited[i]){
                ii p = bipartite(i);
                minS += min(p.first, p.second);
            }
        }
        printf("%d\n", minS);
    }
Beispiel #23
0
int main() {
    ifstream in ;
    ofstream out; in .open("input.txt");
    out.open("output.txt");

    in >> M >> N;
 	char c;
    for (int i = 0; i < M; i++)
        for (int j = 0; j < N; j++) {
           in >> c;
            if (c == '*')
                mappa[i][j] = 0;
            else
                mappa[i][j] = -1;
        }
	cout << "pre TS" << endl;
    dfs(0, 0);
	cout << "post TS" << endl;
	cout << "tsSize: " << ts.size()<< endl;

	mappa[0][0] = 1;
	for(int k= ts.size()-1; k>=0;k--){
		int i = ts[k].first;
		int j = ts[k].second;
		cout << i << " " << j << endl;
		for (int r = 0; r < 2; r++)
        	for (int s = 0; s < 2; s++) {
				if(!(r==0 && s==0) && !(r==1 && s==1))
		        	if (i + r >= 0 && i + r < M && j + s >= 0 && j + s < N)
		        		if (mappa[i + r][j + s] >= 0) {
		        	    	mappa[i + r][j + s] += mappa[i][j];
			    	    }
			}
	}

	 for (int i = 0; i < M; i++){
        for (int j = 0; j < N; j++) 
			cout << mappa[i][j] << " ";
		cout << endl;
	 }
    out << mappa[M - 1][N - 1]; 

	in .close();out.close();

    return 0;
}
Beispiel #24
0
int main(){
    int n, i, k, a,b, maxlis;
    scanf("%d", &n);
    for(i=0; i<n; i++) {
        scanf("%d %d", &a, &b);
        pairs.push_back(make_pair(a,b));
    }

    vii::iterator low;
    lis.push_back(pairs[0]);

    for(i=1, k=1, maxlis=1; i<n; i++){
        if(pairs[i].first>lis[k].first && pairs[i].second>lis[k].second) {
            if(k+1==lis.size())
                lis.push_back(pairs[i]);
            else
                lis[++k]=pairs[i];
            if(k+1>maxlis) maxlis=k+1;
        } else{
            low = lower_bound(lis.begin(), lis.begin()+k+1, pairs[i]);
            lis[k=low-lis.begin()]=pairs[i];
        }
    }
    printf("%d\n", maxlis);
    return 0;
}
void getGraph()
{
	data.assign(2, pii(-1,-1));
	for(int i=0;i < n;i++)
		for(int j=0;j<m;j++)
		{
			if(grip[i][j]=='S')
			{
				data[0]=pii(i,j);
				graph[i][j][0]=graph[i][j][1]=graph[i][j][2]=graph[i][j][3] ='.';
			}	
			else if(grip[i][j]=='G')
			{
				data[1]=pii(i,j);
				graph[i][j][0]=graph[i][j][1]=graph[i][j][2]=graph[i][j][3] ='.';	
			}	
			else
			{ 
				graph[i][j][0]=graph[i][j][1]=graph[i][j][2]=graph[i][j][3] =grip[i][j];
				if(getDir(graph[i][j][0])>=0)
				{
					data.push_back(pii(i,j));
					graph[i][j][1] = fun(graph[i][j][0]);
					graph[i][j][2] = fun(graph[i][j][1]);
					graph[i][j][3] = fun(graph[i][j][2]);
				}	
			}		
		}
	for(int i=2;i<data.size();i++)
	{	
		int dire=getDir(graph[data[i].first][data[i].second][0]);
		for(int k=0;k<4;k++)
		{
			 int dir=(dire+k)%4;
			 int ni=data[i].first+dy[dir];
			 int nj=data[i].second+dx[dir];
			 while(validDisp(ni,nj,k) )
			 {
			 	graph[ni][nj][k]='x';
			 	ni+=dy[dir];
			 	nj+=dx[dir];
			 }
		}
	}
}
int main(){
	int tc, cs=1; cin >> tc;
	while( tc-- ){
		memset(res,0,sizeof res); MP.clear(); 
		mf=0; string s1, s2; adj.assign(MAX,vi());
		
		cin >> m; n=1; s=0; t=MAX-1;
		for(int i=0 ; i<m ; i++){
			cin >> s1; 
			res[mp(s1)][t]=1;
			adj[mp(s1)].push_back(t);
		}
		
		cin >> m; int devs = m;
		for(int i=0 ; i<m ; i++){
			cin >> s1 >> s2; mp(s1);

			res[mp(s1)][mp(s2)]=1;
			adj[mp(s1)].push_back(mp(s2));
			adj[mp(s2)].push_back(mp(s1));
			
			res[s][mp(s1)]=1;
			adj[s].push_back(mp(s1));
		}

		cin >> m;
		for(int i=0 ; i<m ; i++){
			cin >> s1 >> s2;
			res[mp(s1)][mp(s2)]=INF;
			adj[mp(s1)].push_back(mp(s2));
			adj[mp(s2)].push_back(mp(s1));
		}

		
		while(1){
			f=0; memset(par,-1,sizeof par); dist.assign(MAX,INF);
			dist[s]=0; queue<int> q; q.push(s);
			while(!q.empty()){
				int u=q.front(); q.pop();
				if(u==t) break;
				for(int i=0 ; i<adj[u].size(); i++){
					int v=adj[u][i];
					if(res[u][v]>0 && dist[v]==INF){
						dist[v]=dist[u]+1; q.push(v); par[v]=u;
					}
				}
			}
			agument(t,INF);
			if(f==0) break;
			mf+=f;
		}
		cout << devs-mf << endl;
		if(tc) cout << endl;
	}
	return 0;}
void reset()
{
    id = 0;
    ans.clear();
    rep(i,n)
    {
        adj[i].clear();
        check[i] = false;
        low[i] = ind[i] = cnt[i] = 0;
        parent[i] = i;
    }
Beispiel #28
0
void dfs(int i, int j) {
	visited[i][j]=1;
    for (int r = 0; r < 2; r++)
        for (int s = 0; s < 2; s++) {
		    if (i + r >= 0 && i + r < M && j + s >= 0 && j + s < N)
				if(visited[i + r][j + s]==0 && !(r==0 && s==0)&& !(r==1 && s==1))
		        	if (mappa[i + r][j + s] == 0) {
		            	dfs(i + r, j + s);
			        }
        }
	ts.push_back(make_pair(i,j));
}
void createEdges(int kSq){
	int b = blue.size();
	int r = red.size();
	V = b+r+3;
	G.assign(V+2, edgeList());
	for (int i = 0; i < b; ++i) {
		for (int j = 0; j < r; ++j) {
			if(distSq(blue[i],red[j]) <= kSq){
				addEdge(i,j+b);
			}
		}
	}
	S = b+r+1;
	T = b+r+2;
	for (int i = 0; i < b; ++i) {
		addEdge(S,i);
	}
	for (int j = 0; j < r; ++j) {
		addEdge(j+b,T);
	}
}
int main(){
	int t,m,u,v,w;
	scanf("%d",&t);
	while(t--){
		sum=1;
		edges.clear();
		scanf("%d%d",&n,&m);
		for(int i=1;i<=n;i++){
            pSet[i]=i;                      
        }
		for (int i = 0; i < m; ++i)
		{
			scanf("%d%d%d",&u,&v,&w);
			edges.push_back(make_pair(u, make_pair(v,w)));
		}
		sort(edges.begin(),edges.end(),compare);
		for (vector< pair<int,ii> >::iterator i = edges.begin(); i != edges.end(); ++i)
		{
			// printf("%d %d %d\n", i->first,i->second.first,i->second.second);							
			if(!isSameSet(i->first,i->second.first)){
				sum*=(i->second.second);
				if(sum>mod)
					sum=sum%mod;
				unionSet(i->first,i->second.first);
			}
		}			
		printf("%lld\n",sum%mod);
	}
	return 0;
}