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;
}
Exemple #2
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;
}
void primeFactors(ll n, vii &factors) {
    ll PF_idx = 0, PF = primes[PF_idx];
    while (PF * PF <= n) {
        while (n % PF == 0) {
            if(factors.size() == 0)
                factors.push_back(ii(PF, 1));

            else if(factors.back().first == PF)
                factors.back().second++;

            else factors.push_back(ii(PF, 1));
            n /= PF;
        }
        PF = primes[++PF_idx];
    }
    if (n != 1) factors.push_back(ii(n, 1)); // special case if n is a prime
}
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 artiBri(int u) {
    dfs_low[u] = dfs_num[u] = cont++;
    for (auto &i : vec[u]) {
        if (dfs_num[i] == -1) {
            dfs_parent[i] = u;
            artiBri(i);
            if (dfs_low[i] > dfs_num[u])
                bri.push_back(make_pair(u, i));
            dfs_low[u] = min(dfs_low[i], dfs_low[u]);
        } else if (i != dfs_parent[u])
            dfs_low[u] = min(dfs_low[u], dfs_num[i]);
    }
}
Exemple #6
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;
}
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];
			 }
		}
	}
}
Exemple #8
0
bool backtrack(int n){
  bool good=true;
  if(n==N) return true;
  else{
    for(int c=0;c<4;c++){
      good=true;
      for(int i=0;i<AdjList[n].size();i++){
	if(colors[AdjList[n][i].first]==c){
	  good=false;
	  break;
	}
      }
      if(good){
	colors[n]=c;
	sol.push_back(make_pair(n,c));
	if(backtrack(n+1)) return true;
	colors[n]=-1;
      }
    }
    return good;
  }
}
Exemple #9
0
int main() {
    int n, m, u,v,w;
    scanf("%d %d", &n, &m);

    for(int i=0; i<m; i++){
        scanf("%d %d %d", &u, &v, &w);
        l.push_back(ii(w, u));
        graph[u-1].push_back(ii(v-1,w));
    }

    for(int i=0; i<n; i++) cnts[i]=vi(graph[i].size(), -1), sort(graph[i].begin(), graph[i].end(), ii_comp());

    memset(vis, 0, sizeof vis);

    sort(l.begin(), l.end());
    int maxv=0;
    for(int i=0; i<l.size(); i++) if(vis[l[i].second]==0) maxv = max(maxv, dfs(l[i].second, 0));
    printf("%d\n", maxv);

    return 0;

}
Exemple #10
0
int main() {
	freopen("milk2.in", "r", stdin);
	freopen("milk2.out", "w", stdout);

	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		int x, y;
		cin >> x >> y;
		t.push_back(pii(x, y));
	}
	sort(t.begin(), t.end());

	int ans1=0, ans2=0;

	int start = t[0].first; 
	int end = t[0].second;
	ans1 = end - start;
	
	for (int i = 1; i < n; i++)
	{
		if (t[i].first <= end) {
			end = max(end, t[i].second);
		}
		else{
			ans2 = max(t[i].first - end, ans2);
			start = t[i].first;
			end = t[i].second;
		}

		ans1 = max(end - start, ans1);
	}
	
	cout << ans1 << " " << ans2 << endl;

	return 0;
}
Exemple #11
0
int main(){
	string s;
	for (int i = 0; i < 3; i++)
		for (int j = 0; j < 3; j++)
			pos.push_back(ii(i, j));
	int c = 0;
	while (getline(cin, s)) {
//		cout << s << endl;
		c++;
		cout << "Case #" << c << ":" << endl;
		int grid[3][3] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
		for (int i = 0; i < s.size(); i++) {
			int x = pos[s[i]-'a'].first;
			int y = pos[s[i]-'a'].second;
			grid[x][y]++;
			grid[x][y] %= 10;
			for (int j = 0; j < 4; j++) {
				int nx = x + xx[j];
				int ny = y + yy[j];
				if (nx < 0 || nx >= 3 || ny < 0 || ny >= 3)
					continue;
				grid[nx][ny]++;
				grid[nx][ny] %= 10;
			}
		}
		for (int i = 0; i < 3; i++) {
			for (int j = 0;j  < 3; j++) {
				cout << grid[i][j];
				if (j < 2)
					cout << " ";
			}
			cout << endl;
		}
	}
	return 0;
}
 //Imprime el camino de cualquier 
 //nodo v al nodo s -> printPath(v, s)
 void print(ii u, ii s) {
   if (u == s) { path.push_back(s); return; }
   printPath(p[u.fst][u.snd], s);
   path.push_back(u);
 }