Example #1
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;
}
Example #2
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;
	}
Example #3
0
int main() {
    int m;
    scanf("%d", &n);
    for(int i=0; i<n; i++) {
        scanf("%d", &nums[i]);
        nums[i]--;
    }
    scanf("%d", &m);

    newCycles();

    if(fp==m){
    }else if(fp<m){
        while(m-fp>0){
            for(int i=0; i<n && m-fp>0; i++){
                int v=-1;
                for(int j=0; j<n; j++) if(vis[j]!=vis[i]) {v=j; break;}
                if(v!=-1){
                    res.push_back(ii(nums[i], nums[v]));
                    if(res.back().first>res.back().second) swap(res.back().first, res.back().second);
                    swap(nums[i],nums[v]);
                }else break;

                newCycles();
            }
        }
    }else{
        while(fp-m>0){
            for(int i=0; i<n && fp-m>0; i++){
                if(nodeCnt[vis[i]]>1){
                    int v=-1;
                    for(int j=0; j<n; j++) if(vis[j]==vis[i] && j!=i) {v=j; break;}
                    if(v!=-1){
                        res.push_back(ii(nums[i], nums[v]));
                        if(res.back().first>res.back().second) swap(res.back().first, res.back().second);
                        swap(nums[i],nums[v]);
                    }else break;
                }
                newCycles();
            }
        }
    }
    printf("%d\n", res.size());
    if(res.size()){
        printf("%d %d", res[0].first+1, res[0].second+1);
        for(int i=1; i<res.size(); i++) printf(" %d %d", res[i].first+1, res[i].second+1);
        printf("\n");
    }

    return 0;
}
Example #4
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;
}
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;
}
Example #6
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;
}
Example #7
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;
}
Example #8
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;
}
Example #9
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);
}
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);
	}
}
Example #11
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;
}
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;
}
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 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];
			 }
		}
	}
}
Example #15
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;

}
Example #16
0
int recur(int i, int last){
    if(i==m.size()) return 0;
    if(dp[i][last]!=-1) return dp[i][last];
    return dp[i][last]=min(m[i].first-m[last].first+recur(i+1, last), m[i].second+recur(i+1, i));
}
Example #17
0
void print (vii& s) {
    for (int i=0; i<s.size(); i++)
        printf ("(%d,%d) ", s[i].first, s[i].second);
    printf ("\n");
}