Example #1
0
int TheArray::find (int _n, int _d, int _first, int _last) 
{
	arr.clear();
	d = abs(_d);
	n = _n;
	first = _first;
	last = _last;
	if(first > last)
		swap(first, last);

	arr.pb(first);

	while(arr.size() < n){
		lft = n - arr.size();
		if(arr.back() + 0 - d*(lft-1) <= last)
			arr.pb(arr.back() + find1(0, d));
		else
			arr.pb(max(arr.back()-d, last));
	}
/*
	tr(it, arr)
		write(*it);
	cout << endl;
*/

	return *max_element(all(arr));
}
	void compute_hull() {
		int pivot = 0, n = p.size();
		vector<int> ps(n + 1, 0);
		for (int i = 1; i < n; ++i) {
			ps[i] = i;
			if (comp_lexo(p[i], p[pivot])) pivot = i;
		}
		ps[0] = ps[n] = pivot; ps[pivot] = 0;
		sort(ps.begin()+1, ps.end()-1, [this, &pivot](int l, int r) {
			return comp_angl(p[l], p[r], p[pivot]); });

		h.push_back(ps[0]);
		size_t i = 1; ll d;
		while (i < ps.size()) {
			if (p[ps[i]] == p[h.back()]) { i++; continue; }
			if (h.size() < 2 || ((d = det(p[h.end()[-2]],
				p[h.back()], p[ps[i]])) > 0)) { // >= for col.
				h.push_back(ps[i]);
				i++; continue;
			}
			if (p[h.end()[-2]] == p[ps[i]]) { i++; continue; }
			h.pop_back();
			if (d == 0) h.push_back(ps[i]);
		}
		if (h.size() > 1 && h.back() == pivot) h.pop_back();
	}
void tarjanSCC(int u) {
  dfs_low[u] = dfs_num[u] = dfsNumberCounter++;      // dfs_low[u] <= dfs_num[u]
  S.push_back(u);           // stores u in a vector based on order of visitation
  visited[u] = 1;
  for (int j = 0; j < (int)AdjList[u].size(); j++) {
    ii v = AdjList[u][j];
    if (dfs_num[v.first] == DFS_WHITE)
      tarjanSCC(v.first);
    if (visited[v.first])                                // condition for update
      dfs_low[u] = min(dfs_low[u], dfs_low[v.first]);
  }

  if (dfs_low[u] == dfs_num[u]) {         // if this is a root (start) of an SCC
    int total = 0;
    // printf("SCC %d:", ++numSCC);            // this part is done after recursion
    while (1) {
      total++;
      int v = S.back(); S.pop_back(); visited[v] = 0;
      // printf(" %d", v);
      if (u == v){
        if(total == 1 && AdjList[u][0].first != u){
          valid = false;
        }
        break;
      }
    }
    // printf(" TOTAL: %d \n", total);
    clyces.push_back(total % 2 == 0 ? total/2 : total);
  }
}
Example #4
0
void tarjanSCC(int u) 
{
	dfs_low[u] = dfs_num[u] = dfsNumberCounter++; 
	S.push_back(u);

	visited[u] = 1;
	for (int j = 0; j < (int)AdjList[u].size(); j++) 
	{
		int v = AdjList[u][j];
		if (dfs_num[v] == UNVISITED)
			tarjanSCC(v);
		if (visited[v])
			dfs_low[u] = min(dfs_low[u], dfs_low[v]); 
	}
	if (dfs_low[u] == dfs_num[u]) 
	{
		int tam = 0;
		while (1) 
		{
			++tam;
			int v = S.back(); S.pop_back(); visited[v] = 0;
			if (u == v) break; 
		}
		if (tam >= 2) ++numSCC;
	}
}
/**
 * a graph is said to be strongly connected if every vertex is reachable from every other vertex.
 */
void tarjansSCC(int u) {
    dfs_num[u] = dfs_lo[u] = dfsNumberCounter++;
    S.push_back(u);
    visited[u] = true;
    for(int i = 0 ; i < g[u].size() ; i++) {
        int v = g[u][i];

        if(dfs_num[v] == UNVISITED)
            tarjansSCC(v);
        if(visited[v])
            dfs_lo[u] = min(dfs_lo[u], dfs_lo[v]);
    }

    if(dfs_num[u] == dfs_lo[u]) {
        printf("SSC %d:", ++numSCC);
        while(1) {
            int v = S.back();
            S.pop_back();
            visited[v] = false;
            printf(" %d", v);
            if(v == u)break;
        }
        printf("\n");
    }
}
void scc(int u, int p = -1){

	num[u] = lo[u] = cnt++;
	vis[u] = 1;
	cu.push_back(u);
	for(auto v : G[u]){
		if(num[v] == -1)
			scc(v,u);
		if(vis[v])
			lo[u] = min(lo[u], lo[v]);
	}

	if(lo[u]==num[u]){
		cc.push_back(vi());
		int i = cc.size() - 1;

		while(1){
			int v = cu.back();
			vis[v] = false;
			cc[i].push_back(v);
			cu.pop_back();
			if(u == v)break;
		}
	}
}
int main(){
  ios_base::sync_with_stdio(false);
  string linha;
  //~ while(getline(cin,linha)){
    getline(cin, linha);
    //~ cout << linha << endl;
    memset(st, 0, sizeof st);
    cin >> m;
    ans.assign(m, 0);
    beg.assign(m, 0);
    qs.assign(linha.size(), vi());
    int a, b;
    for(int i = 0; i < m; i++){
      cin >> a >> b;
      beg[i] = a-1;
      qs[b-1].push_back(i);
    }
    op.clear();
    int q;
    for(int i = 0; i < (int)linha.size(); i++){
      if(linha[i] == &#39;)&#39;){
        if(op.size() > 0){
          update(1,0,(int)linha.size()-1, op.back());
          op.pop_back();
        }
      }
      for(int j = 0; j < (int)qs[i].size(); j++){
        q = qs[i][j];
        ans[q] = query(1, 0, (int)linha.size()-1, beg[q], i);
      }
      
      if(linha[i] == &#39;(&#39;) op.push_back(i);
    }
Example #8
0
void take() {
    int p = pos.back(); pos.pop_back();
    for (int pi : pos) {
        int d = abs(pi-p);
        cnt[d]++;
    }
}
Example #9
0
void print()
{
    printf("%d\n", (int)a.size());
    forn(j, (int)a.size() - 1)
        printf("%d ", a[j]);
    printf("%d\n", a.back());
    exit(0);
}
Example #10
0
vi merge(vi vec, int i, int j) {
	if (i > j) swap(i, j);
	vec[i] += vec[j];
	swap(vec[j], vec.back());
	vec.pop_back();
	sort(ALL(vec));
	return vec;
}
Example #11
0
int find1(int l, int r) {
	if(l == r)
		return l;
	int mid = (l+r+1)/2;
	if(arr.back() + mid - d*(lft-1) <= last)
		return find1(mid, r);
	else
		return find1(l, mid-1);
}
Example #12
0
void solve() {
	arr.clear();
	p.clear();
/*
	cin >> K >> N;
	arr = vi(K);
	forn(i, K)
		cin >> arr[i];
*/
	cin >> N >> K;
	int64 b, c, r;
	arr = vi(K);
	cin >> arr[0] >> b >> c >> r;

	forab(i, 1, K-1)
		arr[i] = (arr[i-1]*b + c) % r;
	forn(i, K)
		write(arr[i]);
	cout << endl;

	sort(all(arr));
	uni.pb(arr.front());
	p.pb(mp(arr[0], arr[0]));
	
	forn(i, K) {
		if(arr[i] != uni.back())
			uni.pb(arr[i]);
		if(arr[i] == p.back().second)
			;
		else if(arr[i] == p.back().second+1)
			p.back().second++;
		else
			p.pb(mp(arr[i], arr[i]));

	}

	forn(i, K)
		write(arr[i]);
	cout << endl;
	forn(i, uni.size())
		write(uni[i]);
	cout << endl;

	
	tr(it, p)
		Pf("(%d, %d) ", it->first, it->second);
	cout << endl;

	int M = N;
	forab(i, K, N) {
		N = i;
		Pf("arr[%d] = %d\n", N, binarySearch());
	}
int main (){
	scanf("%d%d", &n, &k);
	for (int i = 0; i < n * k / 2; i++){
		scanf("%d%d", &x, &y);
		a[--x][--y] = a[y][x] = 1;
		st[x]++;st[y]++;
		if (x > y)swap(x,y);
		edge.push_back(make_pair(x,y));
	}
	for (int i = 0; i < n; i++)
		if (st[i]){
			euler(i);
			for (int j = 1; j < eu.size(); j++)
				g[eu[j - 1]].push_back(eu[j]);
			if (eu.back() != eu.front())
				g[eu.back()].push_back(eu.front());
			eu.clear();
		}
	mt.assign(n, -1);
	for (int i = 0; i < n; i++){
		u.assign(n, 0);
		dfs(i);
	}
	for (int i = 0; i < n; i++)
		ans += mt[i] != -1;
	if (ans == n){
		puts("YES");
		for (int i = 0; i < n; i++){
			x = i, y = mt[i];
			if (x > y)swap(x,y);
			for (int j = 0; j < edge.size(); j++)
				if (x == edge[j].first && y == edge[j].second){
					printf("%d\n", j + 1);break;}
		}
	} else puts("NO");
	return 0;
}
Example #14
0
void tarjanSCC(int u){
    dfs_num[u]=dfs_low[u]=dfsC++;
    S.push_back(u);
    vis[u]=1;
    for(int i=0; i<graph[u].size(); i++){
        if(dfs_num[graph[u][i]]==-1) tarjanSCC(graph[u][i]);
        if(vis[graph[u][i]]) dfs_low[u] = min(dfs_low[u], dfs_low[graph[u][i]]);
    }
    if(dfs_low[u]==dfs_num[u]){
        scc[u]=numSCC;
        while(1){
            int v = S.back(); S.pop_back();
            vis[v]=0;
            scc[v]=numSCC;
            if(u==v) break;
        }
        numSCC++;
    }
}
void tarjanSCC(int u) {
    dfs_low[u] = dfs_num[u] = dfsNumberCounter++; // dfs_low[u] <= dfs_num[u]
    S.push_back(u); // stores u in a vector based on order of visitation
    visited[u] = 1;
    for (int j = 0; j < (int)AdjList[u].size(); j++) {
        ii v = AdjList[u][j];
        if (dfs_num[v.first] == 0)
            tarjanSCC(v.first);
        if (visited[v.first])
            dfs_low[u] = min(dfs_low[u], dfs_low[v.first]); }
    if (dfs_low[u] == dfs_num[u]) { // if this is a root (start) of an SCC
        scc.push_back(vi());
        while (1) {
            int v = S.back(); S.pop_back(); visited[v] = 0;
            scc.back().push_back(v);
            node_to_scc_num[v] = scc_num;
            if (u == v) break; }
        scc_num++;
    }
 }
Example #16
0
void tarjanSCC(int u) {
  dfs_low[u] = dfs_num[u] = dfsNumberCounter++;      // dfs_low[u] <= dfs_num[u]
  S.push_back(u);           // stores u in a vector based on order of visitation
  visited[u] = 1;
  for (int j = 0; j < (int)AdjList[u].size(); j++) {
    ii v = AdjList[u][j];
    if (dfs_num[v.first] == DFS_WHITE)
      tarjanSCC(v.first);
    if (visited[v.first])                                // condition for update
      dfs_low[u] = min(dfs_low[u], dfs_low[v.first]);
  }

  if (dfs_low[u] == dfs_num[u]) {         // if this is a root (start) of an SCC
    ++numSCC;
    while (1) {
      int v = S.back(); S.pop_back(); visited[v] = 0;
      if (u == v) break;
    }
  }
}
void tarjanSCC(int u){
    dfs_num[u]=dfs_low[u]=dfs_counter++;
    visited[u]=1;
    S.push_back(u);
    for(int j=0;j<(int)AdjList[u].size();j++){
        ii v=AdjList[u][j];
        if(dfs_num[v.first]==DFS_WHITE)
            tarjanSCC(v.first);
        if(visited[v.first])
            dfs_low[u]=min(dfs_low[u],dfs_low[v.first]);
    }

    if(dfs_low[u]==dfs_num[u]){
       numSCC++;
       while(1){
         int v=S.back(); S.pop_back(); visited[v]=0;
         if(v==u) break;
       }
    }
}
void tarjanSCC(int u)
{
    dfs_low[u]=dfs_num[u]=++dfsNumberCounter;
    S.push_back(u);
    visited[u]=1;
    for(int j=0;j<(int)AdjList[u].size();j++){
        int v=AdjList[u][j];
        if(dfs_num[v]==0) tarjanSCC(v);
        if(visited[v]==1)
            dfs_low[u]=min(dfs_low[u],dfs_low[v]);
    }
    if(dfs_low[u]==dfs_num[u]){                  // if this is a root (start) of an SCC
        while(1){
            int v=S.back(); S.pop_back();visited[v]=0;
            belong[v]=SCC;
            if(u==v) break;
        }
        SCC++;
    }
}
Example #19
0
void tarjanSCC(int u) {
	dfs_low[u] = dfs_num[u] = dfsNumberCounter++;
	S.push_back(u);
	visited[u] = 1;
	for (int j = 0; j < g[u].size(); j++) {
		int v = g[u][j];
		if (dfs_num[v] == DFS_WHITE) tarjanSCC(v);
		if (visited[v]) dfs_low[u] = min(dfs_low[u], dfs_low[v]);
	}
	if (dfs_low[u] == dfs_num[u]) {
		cout << "SCC " << ++numSCC << ":";
		while (1) {
			int v = S.back();
			S.pop_back();
			visited[v] = 0;
			cout << " " << v;
			if (u == v) break;
		}
		cout << endl;
	}
}
void tarjan(int u){
  dfs_low[u] = dfs_num[u] = dfsNumberCounter++;
  S.push_back(u);
  visited[u] = 1;
  for(int i = 0; i < (int)AdjList[u].size(); i++){
    int v = AdjList[u][i];
    if(dfs_num[v] == 0)
      tarjan(v);
    if(visited[v])
       dfs_low[u] = min(dfs_low[u], dfs_low[v]);
  }

  if(dfs_low[u] == dfs_num[u]){
    numSCC++;
    while(true){
      int v = S.back(); visited[v] = 0; S.pop_back();
      if(u == v)
	break;
    }
  }
}
void tarjanSCC(int u) {
	dfs_low[u] = dfs_num[u] = dfsCounter++;      // dfs_low[u] <= dfs_num[u]
	S.push_back(u);           // stores u in a vector based on order of visitation
	visited[u] = 1;
	for(auto &v :AdjList[u]) {
		if (dfs_num[v] == DFS_WHITE)
			tarjanSCC(v);
		if (visited[v])                                // condition for update
			dfs_low[u] = min(dfs_low[u], dfs_low[v]);
	}

	if (dfs_low[u] == dfs_num[u]) {         // if this is a root (start) of an SCC
		while (1) {
			int v = S.back(); S.pop_back(); visited[v] = 0;
			scc_id[v] = numSCC;
			scc_weight[numSCC]--;
			if (u == v) break;
		}
		numSCC++;
	}
}
Example #22
0
void build(int n, vector<set<int> >& adjacent_nodes, vector<vi>& solutions, vi& solution) {
  int start = solution.size() ? solution.back()+1 : 0;
  bool add_solution = true;
  for (int i=start;i<n;++i) {
    bool has_adjacent = false;
    for (int j=0;j<solution.size();++j) {
      if (adjacent_nodes[i].find(solution[j])!=adjacent_nodes[i].end()) {
        has_adjacent = true;
        break;
      }
    }
    if (!has_adjacent) {
      add_solution = false;
      solution.push_back(i);
      build(n,adjacent_nodes,solutions,solution);
      solution.pop_back();
    }
  }

  if (add_solution) {
    solutions.push_back(solution);
  }
}
void tarjanSCC(int u) {
    dfs_low[u] = dfs_num[u] = dfsNumberCounter++;      // dfs_low[u] <= dfs_num[u]
    S.push_back(u);           // stores u in a vector based on order of visitation
    visited[u] = 1;
    for (int j = 0; j < (int)grap[u].size(); j++) {
        int v = grap[u][j];
        if (dfs_num[v] == DFS_WHITE)
            tarjanSCC(v);
        if (visited[v])                                // condition for update
            dfs_low[u] = min(dfs_low[u], dfs_low[v]);
    }

    if (dfs_low[u] == dfs_num[u]) {         // if this is a root (start) of an SCC
        int total = 0;
        while (1) {
            total++;
            int v = S.back();
            S.pop_back();
            visited[v] = 0;
            if (u == v || total > 1) break;
        }
        if(total > 1) cycle = true;
    }
}
Example #24
0
File: b.cpp Project: MannyFM/ACMP
void out(vi & a)
{
	printf("%d", a.empty() ? 0 : a.back());
	for (int i = int(a.size()) - 2; i >= 0; i--)
		printf("%09d", a[i]);
}
Example #25
0
void fix(vi &a) {
	for (int i=0; i+1<a.size(); i++) if (a[i]>=M) 
		{ a[i+1]+=a[i]/M; a[i]%=M; }
	while (a.size()>=2 && a.back()==0) 
		a.pop_back();
}
int getR(){
	return (vec.back() - vec.front())/(vec.size());
}