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;
}
예제 #2
0
파일: milk.cpp 프로젝트: ABHINAVKR/ACM
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;
}
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;
 		}
 	}
예제 #4
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;
}
예제 #5
0
int main() {
    int n;
    cin >> n;
    vector<pii> x(n);
    for (int i = 0; i < n; ++i) {
        cin >> x[i].first >> x[i].second;
    }
    sort(x.begin(), x.end());
    res = x;
    out(x, 0, x.size());
/*    for (int i = 0; i < res.size(); ++i)
        swap(res[i].first, res[i].second);
    res = out(res);
    for (int i = 0; i < res.size(); ++i)
        swap(res[i].first, res[i].second);
    res.push_back(pii(-1e9 - 10, 0));
    res = out(res);*/
    sort(res.begin(), res.end());
    res.resize(unique(res.begin(), res.end()) - res.begin());
    cout << res.size() << endl;
    for (int i = 0; i < res.size(); ++i)
        cout << res[i].first << " " << res[i].second << endl;
    return 0;
}
예제 #6
0
파일: C38E.cpp 프로젝트: ABHINAVKR/ACM
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;

}
예제 #7
0
파일: BRIDGE.cpp 프로젝트: ABHINAVKR/ACM
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;
}
예제 #8
0
int main() {
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  scanf("%d %d", &k, &n);
  for(int i = 0; i < n; ++i) {
    scanf("%d", c + i);
    foo.pb(mp(c[i], i));
  }
  sort(foo.begin(), foo.end());
  for(int i = 0; i < n; ++i) {
    int id = foo[i].S, x = foo[i].F;
    int mini = min(x, k);
    k -= mini;
    sol[id] = mini;
  }
  for(int i = 0 ; i < n; ++i) {
    if(i) printf(" ");
    printf("%d", sol[i]);
  }
  printf("\n");
  return 0;
}
예제 #9
0
파일: C261C.cpp 프로젝트: ABHINAVKR/ACM
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;

}
예제 #10
0
파일: milk2.cpp 프로젝트: bozhou/train
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;
}
예제 #11
0
int main(){

    ios::sync_with_stdio(0);

    int n;
    while(cin >> n) {
        int a, num, b;
        char c;
        cont = 0;
        dfs_num.clear();
        dfs_num.resize(n, -1);
        dfs_low.clear();
        dfs_low.resize(n);
        dfs_parent.clear();
        dfs_parent.resize(n);
        vec.clear();
        vec.resize(n);
        for (int i = 0; i < n; ++i) {
            cin >> a >> c >> num >> c;
            for (int j = 0; j < num; ++j) {
                cin >> b;
                vec[a].push_back(b);
            }
        }
        for (int k = 0; k < n; ++k)
            if (dfs_num[k] == -1)
                artiBri(k);
        sort(bri.begin(), bri.end());
        cout << bri.size() << " critical links" << endl;
        for (int l = 0; l < bri.size(); ++l)
            cout << bri[l].first << " - " << bri[l].second << endl;

    }

    return 0;
}