コード例 #1
0
void st_create(vi &st, const vi &a)
{
  int size = (int)(2 * pow(2.0, floor((log((double)a.size()) / log(2.0) + 1))));
  st.assign(size, 0);
  st_build(st, a, 1, 0, (int)a.size() - 1);
}
コード例 #2
0
int main() {
  int V, total_neighbors, id, weight;

  /*
  // Use the following input:
  // Graph in Figure 4.1
  9
  1 1 0
  3 0 0 2 0 3 0
  2 1 0 3 0
  3 1 0 2 0 4 0
  1 3 0
  0
  2 7 0 8 0
  1 6 0
  1 6 0

  // Example of directed acyclic graph in Figure 4.4 (for toposort)
  8
  2 1 0 2 0
  2 2 0 3 0
  2 3 0 5 0
  1 4 0
  0
  0
  0
  1 6 0

  // Example of directed graph with back edges
  3
  1 1 0
  1 2 0
  1 0 0

  // Left graph in Figure 4.6/4.7/4.8
  6
  1 1 0
  3 0 0 2 0 4 0
  1 1 0
  1 4 0
  3 1 0 3 0 5 0
  1 4 0

  // Right graph in Figure 4.6/4.7/4.8
  6
  1 1 0
  5 0 0 2 0 3 0 4 0 5 0
  1 1 0
  1 1 0
  2 1 0 5 0
  2 1 0 4 0

  // Directed graph in Figure 4.9
  8
  1 1 0
  1 3 0
  1 1 0
  2 2 0 4 0
  1 5 0
  1 7 0
  1 4 0
  1 6 0
  */

  freopen("in_01.txt", "r", stdin);

  scanf("%d", &V);
  AdjList.assign(V, vii()); // assign blank vectors of pair<int, int>s to AdjList
  for (int i = 0; i < V; i++) {
    scanf("%d", &total_neighbors);
    for (int j = 0; j < total_neighbors; j++) {
      scanf("%d %d", &id, &weight);
      AdjList[i].push_back(ii(id, weight));
    }
  }

  printThis("Standard DFS Demo (the input graph must be UNDIRECTED)");
  numCC = 0;
  dfs_num.assign(V, DFS_WHITE);    // this sets all vertices' state to DFS_WHITE
  for (int i = 0; i < V; i++)                   // for each vertex i in [0..V-1]
    if (dfs_num[i] == DFS_WHITE)            // if that vertex is not visited yet
      printf("Component %d:", ++numCC), dfs(i), printf("\n");   // 3 lines here!
  printf("There are %d connected components\n", numCC);

  printThis("Flood Fill Demo (the input graph must be UNDIRECTED)");
  numCC = 0;
  dfs_num.assign(V, DFS_WHITE);
  for (int i = 0; i < V; i++)
    if (dfs_num[i] == DFS_WHITE)
      floodfill(i, ++numCC);
  for (int i = 0; i < V; i++)
    printf("Vertex %d has color %d\n", i, dfs_num[i]);

  // make sure that the given graph is DAG
  printThis("Topological Sort (the input graph must be DAG)");
  topoSort.clear();
  dfs_num.assign(V, DFS_WHITE);
  for (int i = 0; i < V; i++)            // this part is the same as finding CCs
    if (dfs_num[i] == DFS_WHITE)
      dfs2(i);
  reverse(topoSort.begin(), topoSort.end());                 // reverse topoSort
  for (int i = 0; i < (int)topoSort.size(); i++)       // or you can simply read
    printf(" %d", topoSort[i]);           // the content of `topoSort' backwards
  printf("\n");

  printThis("Graph Edges Property Check");
  numCC = 0;
  dfs_num.assign(V, DFS_WHITE); dfs_parent.assign(V, -1);
  for (int i = 0; i < V; i++)
    if (dfs_num[i] == DFS_WHITE)
      printf("Component %d:\n", ++numCC), graphCheck(i);       // 2 lines in one

  printThis("Articulation Points & Bridges (the input graph must be UNDIRECTED)");
  dfsNumberCounter = 0; dfs_num.assign(V, DFS_WHITE); dfs_low.assign(V, 0);
  dfs_parent.assign(V, -1); articulation_vertex.assign(V, 0);
  printf("Bridges:\n");
  for (int i = 0; i < V; i++)
    if (dfs_num[i] == DFS_WHITE) {
      dfsRoot = i; rootChildren = 0;
      articulationPointAndBridge(i);
      articulation_vertex[dfsRoot] = (rootChildren > 1); }       // special case
  printf("Articulation Points:\n");
  for (int i = 0; i < V; i++)
    if (articulation_vertex[i])
      printf(" Vertex %d\n", i);

  printThis("Strongly Connected Components (the input graph must be DIRECTED)");
  dfs_num.assign(V, DFS_WHITE); dfs_low.assign(V, 0); visited.assign(V, 0);
  dfsNumberCounter = numSCC = 0;
  for (int i = 0; i < V; i++)
    if (dfs_num[i] == DFS_WHITE)
      tarjanSCC(i);

  return 0;
}
コード例 #3
0
ファイル: 12532.cpp プロジェクト: orlandoaceto/UVaOnlineJudge
		SegmentTree(const vector<int> &_A){
			A = _A; n = (int)A.size();
			st.assign(4 * n, 0);
			build(1, 0, n-1);
		}
コード例 #4
0
		SegmentTree(vi &_A){
			size = (int)_A.size();
			A = _A;
			tree.assign(size*4,0);
			build(1,0,size-1);
		}
 SegmentTree(const vi &_A){
   A = _A; n = (int) A.size();
   st.assign(n * 4, 0);
   lazy.assign(n * 4, -1);
   build(1, 0, n - 1);
 }
コード例 #6
0
ファイル: 12345.cpp プロジェクト: giusevtr/problemsolving
int main(){
	FASTER;

	cin >> n >> q;
	vi W(3000000,0);
	ft.assign(6000000,0);
	for (int i = 0; i < n; ++i) {
		int t;
		cin >> t;
		v.push_back(t);
		W[t]++;
		if(query(t,t) == 0){
			update(t,1);
		}
	}
	st1.assign(20000000,0);
	st2.assign(20000000,0);
	buildMax(1,0,v.size()-1);
	buildMin(1,0,v.size()-1);


	for (int i = 0; i < q; ++i) {
		char c;
		int x,y;
		cin >> c >>x >> y;

		if(c == 'Q'){
			y--;
			int maxi = queryMax(1,0,v.size()-1, x,y);
			int mini = queryMin(1,0,v.size()-1, x,y);

			printf("range (%d,%d) = %lld %lld\n",mini,maxi, v[mini], v[maxi]);

			ll cnt = query(v[mini],v[maxi]);

			cout << cnt << endl;
		}else{
			ll val = v[x];

			if(W[val]){
				printf("remove %lld\n", val);
				W[val]--;

				if(W[val] == 0 && query(val,val) == 1){
					update(val,-1);
				}
			}

			W[y]++;

			if(query(y,y) == 0){
				update(y,1);
			}

			updateMax(1,x,0,(int)v.size()-1,y);
			updateMin(1,x,0,(int)v.size()-1,y);
		}
	}


	return 0;
}
コード例 #7
0
int main(){
	FASTER;

	int n,m;

	while(cin >> n >> m, n || m){
		A.assign(n+1,0);
		for (int i = 1; i <= n; ++i) {
			cin >> A[i];
		}

		int s = 0;
		MEM(C,0);
		// pre-compute
		for (int i = 1; i <= n; ++i) {
			s = A[i];
			for (int j = i+1; j <= n; ++j) {
				C[i][j] = s * A[j] + C[i][j-1];
				s += A[j];
			}
		}

		if(m == 0){
			cout << C[1][n] << endl;
			continue;
		}

		MEM(dp,0);

		for (int i = 0; i <= n; ++i)
			for (int j = 0; j <= m+1; ++j)
				dp[i][j] = 1e9;

		// Slow
//		dp[0][0] = 0;
//		for (int i = 1; i <= n; ++i) {
//			for (int j = 1; j <= i; ++j) {
//				for (int k = 1; k <= i; ++	k) {
//					int tmp = dp[k-1][j-1] + C[k][i];
//					dp[i][j] = min(dp[i][j], tmp);
//				}
//			}
//		}
//		cout << dp[n][m+1] << endl;

		// Fast
		dp[n+1][0] = 0;
		for (int i = n; i >= 1; --i) {
			for (int j = 1; j <= n; ++j) {
				for (int k = i; k <= n; ++k) {
					int tmp = dp[k+1][j-1] + C[i][k];

					// optimization
					if(C[i][k] > dp[i][j] )break;
					dp[i][j] = min(dp[i][j], tmp);
				}
			}
		}

		cout << dp[1][m+1] << endl;
	}


	return 0;
}