예제 #1
0
int main()
{
    FILE *in  = fopen (PROBLEM_NAME".in","r");
    FILE *out = fopen (PROBLEM_NAME".out","w");

    int k;
    fscanf(in, "%d %d ", &n, &k);
    for (int i = 0; i < n; ++i) {
        used.push_back(false);
        tin.push_back(0);
        fup.push_back(0);
        vi l;
        g.push_back(l);
    }
    for (int i = 0; i < k; ++i) {
        int x, y;
        fscanf(in, "%d %d ", &x, &y);
        g[x-1].push_back(y-1);
        g[y-1].push_back(x-1);
    }

    timer = 0;
    dfs(0);

    sort(ans.begin(), ans.end());
    ans.erase( unique( ans.begin(), ans.end() ), ans.end() );
    fprintf(out, "%d\n", (int)ans.size());
    for (size_t i = 0; i < ans.size(); ++i) {
        fprintf(out, "%d\n", ans[i]+1);
    }

    fclose(in);
    fclose(out);
    return 0;
}
예제 #2
0
int main(){
  scanf("%d%d", &N, &M);
  int a;
  for(int i = 0; i < N; ++i){
    scanf("%d", &a);
    cities.push_back(a);
  }
  for(int i = 0; i < M; ++i){
    scanf("%d", &a);
    towers.push_back(a);
  }

  c = 0;
  t = 1;
  if(towers[0] > cities[0]){
    r = towers[0] - cities[0];
    c = 1;
  }
  while(c<N){
    while(cities[c] > towers[t] && t < M) t++;
    int d = cities[c] - towers[t-1];
    if(t < M) d = min(d, towers[t] - cities[c]);
    r = max(r,(ll) d);
    c++;
  }
  cout << r << endl;
}
void bfs(int u) {
	col[0].clear();
	col[1].clear();
	queue<int> q;
	q.push(u);
	vis[u] = 0;
	col[0].push_back(u);
	while (!q.empty()) {
		int u = q.front();
		q.pop();
		for (int i = 0; i < sz(g[u]); i++) {
			int v = g[u][i];
			if (vis[v] == -1) {
				vis[v] = !vis[u];
				q.push(v);
				col[vis[v]].push_back(v);
			}
		}
	}
	if (sz(col[0]) < sz(col[1])) {
		for (int i = 0; i < sz(col[0]); i++)
			ans.push_back(col[0][i]);
	} else {
		for (int i = 0; i < sz(col[1]); i++)
			ans.push_back(col[1][i]);
	}
}
예제 #4
0
int main () {
	ios_base::sync_with_stdio(0); cin.tie(0);
	//cout<<fixed<<setprecision(7); cerr<<fixed<<setprecision(7); //cin.ignore(INT_MAX, ' '); //cout << setfill('0') << setw(5) << 25
	int tc = 1, i, j;
	
	for (i=3; i<1100; i++) if (!sieve[i])
		for (j=i*i; j<1000100; j+=i+i)
			sieve[j] = true;
	primes.push_back(2);
	for (i=3; i<1000100; i+=2) if (!sieve[i]) primes.push_back(i);
	for (i=1; i<primes.size(); i++) difs[primes[i]-primes[i-1]].push_back(ii(primes[i-1], primes[i]));
	cin>>tc;
	while (tc--) {
		cin>>l>>r;
		mx = -1; cnt = 0;
		for (pair<int, vii> d : difs) {
			auto itrl = lower_bound(d.second.begin(), d.second.end(), ii(l, 0), cmp_first);
			auto itrr = upper_bound(d.second.begin(), d.second.end(), ii(0, r), cmp_second);
			if (itrl == d.second.end() || itrr == d.second.begin()) continue;
			itrr--;
			int dis = distance(itrl, itrr);
			if (dis > mx) {
				mx = dis;
				cnt = 1;
				ans = d.first;
			} else if (dis == mx) cnt++;
		}
		if (cnt != 1) cout<<"No jumping champion"<<endl;
		else cout<<"The jumping champion is "<<ans<<endl; 
	}
	
	return 0;
}
int main(){
	// Test
	int d, Case =1;
	while(scanf("%d", &n) && n){
		t.clear();s.clear();
		for (int i = 1; i <= n; ++i)
			s.push_back(i);

		for (int i = 0, tmp; i < n; ++i){
			scanf("%d", &tmp);
			t.push_back(tmp);
		}

		printf("Case %d: ",Case++);

		if((d = bfs(t,s,2,dist_t,dist_s)) != INF){
			printf("%d\n", d);
		}else if((d = bfs(s,t,2,dist_s,dist_t)) != INF){
			printf("%d\n", d);
		}else
			printf("5\n");

	}

	return 0;
}
예제 #6
0
int main() {
  fibo.push_back(0);
  int A = 1;
  int B = 2;
  fibo.push_back(1);
  fibo.push_back(2);
  int C;
  
  do {
    C=A+B;
    if(C > MAXVAL) break;
    fibo.push_back(C);
    A=B;
    B=C;
  }while(true);
  
  int T;
  int X,K;

  scanf("%d",&T);
  while(T--) {
    scanf("%d %d",&X,&K);
    printf("%llu\n",part(X,fibo.size()-1,K));
  }
  return 0;
}
예제 #7
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
    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);
  }
}
	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();
	}
int main(){
	findprimes();
	ll n, res;
	cin >> n;
	while(n!= 0){
		res = n;
		
		if(n < 0){
			results.push_back(-1);
			res = res/-1;
		}
		int idx = 0;
		while(res != 1){
			DEBUG(res);
			if(res%primos[idx] == 0){
				res = res/primos[idx];
				results.push_back(primos[idx]);
			}else{
				idx++;
			}
		}
		printf("%d = ", n);
		REP(i, results.size()-1) printf("%d x ", results[i]);
		printf("%d\n", results[results.size()-1]);
		
		
		cin >> n;
		results.clear();
	}
	
	
}
예제 #10
0
파일: MENU.cpp 프로젝트: ABHINAVKR/ACM
void reconstruct(int i, int last, int b){
    if(i>=k) return;

    int choice=0, level=0;

    di cur, maxv(-1, -1);
    for(int j=1; j<=n; j++){
        if(c[j]<=b && j!=last) {
            cur = dp[i+1][j][b-c[j]];
            cur.first += v[j];
            if(cur.first>maxv.first || fabs(cur.first-maxv.first)<EPS) {
                if(fabs(cur.first-maxv.first)<EPS) {
                    if(cur.second > maxv.second) { maxv=cur; choice=j; level=0; }
                }else { maxv=cur; choice=j; level=0; }
            }
            if(i<k-1 && 2*c[j]<=b){
                cur = dp[i+2][j][b-2*c[j]];
                cur.first += 1.5*v[j];
                if(cur.first>maxv.first || fabs(cur.first-maxv.first)<EPS) {
                    if(fabs(cur.first-maxv.first)<EPS) {
                        if(cur.second>maxv.second) { maxv=cur; choice=j; level=1; }
                    }else { maxv=cur; choice=j; level=1; }
                }
            }
        }
    }
    ans.push_back(choice);
    if(level==0) reconstruct(i+1, choice, b-c[choice]);
    else { ans.push_back(choice); reconstruct(i+2, choice, b-2*c[choice]); }
}
예제 #11
0
void convex_hull(vector<Point> & a) {
	if (a.size() == 1)
		return;
	sort(a.begin(), a.end(), &cmp);
	Point p1 = a[0], p2 = a.back();
	vector<Point> up, down;
	up1.clear();
	down1.clear();
	up.push_back(p1); up1.pb(0);
	down.push_back(p1); down1.pb(0);
	for (size_t i = 1; i < a.size(); ++i) {
		if (i == a.size() - 1 || ccw(p1, a[i], p2)) {
			while (up.size() >= 2 && !ccw(up[up.size() - 2], up[up.size() - 1], a[i]))
				up.pop_back(),up1.pop_back();
			up.push_back(a[i]),up1.push_back(i);
		}
		if (i == a.size() - 1 || cw(p1, a[i], p2)) {
			while (down.size() >= 2 && !cw(down[down.size() - 2], down[down.size() - 1], a[i]))
				down.pop_back(),down1.pop_back();
			down.push_back(a[i]),down1.push_back(i);
		}
	}
	if (up[0].x == up.back().x && up[0].y == up.back().y)
		up.erase(up.end() - 1),up1.erase(up1.end()-1);
	a.clear();a1.clear();	
	for (size_t i = 0; i < up.size(); ++i)
		a.push_back(up[i]),a1.push_back(up1[i]);
	for (size_t i = down.size() - 2; i > 0; --i)
		a.push_back(down[i]),a1.push_back(down1[i]);
}
예제 #12
0
int main(){
  scanf("%d%d", &N, &M);
  int a;
  for(int i = 0; i < N; ++i){
    scanf("%d", &a);
    cities.push_back(a);
  }
  for(int i = 0; i < M; ++i){
    scanf("%d", &a);
    towers.push_back(a);
  }
  s = 0;
  e = INF;
  
  while(s < e){
    r = (s+e)/2;
    bool covered = true;
    for(int i = 0; i < N; i++){
      int low = lower_bound(towers.begin(), towers.end(), cities[i]) - towers.begin();
      int high = upper_bound(towers.begin(), towers.end(), cities[i]) - towers.begin();
      if(towers[low] == cities[i]) continue;
      if(low != 0) low--; // the one before cities[i]
      if(high == M) high--;
      if(abs(towers[low] - cities[i]) <= r || abs(towers[high] - (ll) cities[i]) <= r) continue;
      covered = false;
      break;
    }
    if(covered){
      e = r;
    }else{
      s = r+1; 
    }
  }
  cout << s << endl;
}
void eratostenes(int n, vi &p) {
	// Inicializando a lista de candidatos a primos.
	vi l;

	for (int i = 0; i < n / 2; i++) {
		l.push_back(2 * i + 3);
	}

	// Para cada candidato.
	for (int i = 0; i < n / 2; i++) {
		// Como cortamos os múltiplos de dois, precisamos mapear os índices.
		int c = l[i];

		if (c != -1) {
			// Como eu só começo a marcar a partir de c * c.
			if (c * c > n) {
				break;
			}

			// Começando a cortar a partir do quadrado de l[i] = c.
			for (int idx = (c * c - 3) / 2; idx < n / 2; idx += c) {
				l[idx] = -1;
			}
		}
	}

	// Vendo os primos.
	if (n >= 2) p.push_back(2);

	for (int i = 0; i < n / 2; i++) if (l[i] != -1) p.push_back(l[i]);
}
예제 #14
0
파일: D.cpp 프로젝트: lyoz/contest
			rep(j,sw){
				if(grid[i][j]=='#'){
					is.push_back(i);
					is.push_back(i+h-sh+1);
					js.push_back(j);
					js.push_back(j+w-sw+1);
				}
			}
예제 #15
0
int main()
{
    int n;
    int ans=1, count=1, pos=1, ind=1;
    while(scanf("%d", &n)!=EOF)
    {
        v.push_back(n);
        indexes.push_back(1);
        ++count;
    }
    int seq[count];
    seq[1]=v[0];
    for(int i=1;i<v.size();++i)
    {
        if(v[i]<seq[1])
        {
            seq[1]=v[i];
            indexes[i]=1;
        }
        else
        if(v[i]>seq[pos])
        {
            seq[++pos]=v[i];
            indexes[i]=pos;
        }
        else
        {
            int place=binary_search_find(seq, pos, v[i]);
            seq[place]=v[i];
            indexes[i]=place;
        }
        if(indexes[i]>=ans)
        {
            ans=indexes[i];
            ind=i;
        }
        
    }
    int p=ans;
    for(int i=ind;i>=0 && p>0;i--)
    {
        if(indexes[i]==p)
        {
            rev.push_back(v[i]);
            --p;
        }
    }
    printf("%d\n-\n", ans);
    for(int i=rev.size()-1;i>=0;i--)
    {
        printf("%d\n", rev[i]);
    }
    return 0;
}
void init () {
	int i, j;
	memset(sieve, -1, sizeof sieve);
	for (i=3; i<50; i+=2)
		if (sieve[i])
			for (j=i*i; j<250; j+=i+i)
				sieve[j] = false;
	primes.push_back(2);
	for (i=3; i<250; i+=2)
		if (sieve[i])
			primes.push_back(i);
}
예제 #17
0
void calculate_primes () {
	bool sieve[1005];
	memset(sieve, -1, sizeof sieve);
	for (int i=3; i<400; i+=2)
		if (sieve[i])
			for (int j=i*i; j<1005; j+=i+i)
				sieve[j] = false;
	primes.push_back(2);
	for (int i=3; i<1005; i+=2)
		if (sieve[i])
			primes.push_back(i);
}
void sieve(){
	P.clear();
	memset(C, 0, sizeof C);
	P.push_back(2);
	int i, j;
	for(i = 3; i < MAXN; i+=2) if(!C[i]) {
		P.push_back(i);
		for(j = i+i; j < MAXN; j+=i){
			C[j] = true;
		}
	}
}
예제 #19
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;
	}
예제 #20
0
bool getInput()
{
    //get input
    scanf("%d %d ", &n, &m);
    if (!n && !m) return false;
    int a, b, c;
    REP(i, m)
    {
        scanf("%d %d %d ", &a, &b, &c);
        rules.push_back(a);
        rules.push_back(b);
        rules.push_back(c);
    }
예제 #21
0
inline void iv(vi &v,int n){
  for (int i = 0; i < n; ++i) {
    int temp;
    scanf("%d",&temp);
    v.push_back(temp);
  }
}
void backtracking(vi s,vi steps, int d){
	if(vist.find(s) != vist.end() || d > limit){
//		printf("return d = %d limit = %d\n", d, limit);
		return;
	}
	if(done || isDone(s)){
		if(!done){
//			printf("found!\n");
			ans = steps;
		}
		done = true;

		return;
	}
	vist[s] = 1;

	int cost = d + H(s);
	printf("H(s) = %d d = %d \n", H(s), d);
	printf("best = %d\tcost = %d\n", best,cost);
	debug(s);
	if(cost <= best){
		best = cost;
		for (int i = 0; i < 4; ++i) {
			vi t = s;
			if(move(t, i)){
				printf("moving %c\n", toc(i));
				steps.push_back(i);
				backtracking(t,steps,d + 1);
				steps.pop_back();
			}
		}
	}
}
예제 #23
0
int main(){
	FASTER;
	n = 12;

	while(1){
		values.clear();
		bool end = false;
		for (int i = 0,t; i < 12; ++i) {
			cin >> t;
			values.push_back(t);
			end = end || t>0;
		}
		if(!end)break;

		permu.assign(12, 0);
		vis.assign(12, 0);

		vis[0] = 1;
		permu[0] = values[0];
		cnt = 0;

		go(0);

		permu.assign(12, 0);
		permu[1] = values[0];

		go(0);

		cout << cnt/2 << endl;

	}

	return 0;
}
예제 #24
0
void cycleCheck(int u, int par) {
    num[u] = EXPLORED;
    path.push_back(u);
    for (int i = 0; i < g[u].size(); i++) {
        int v = g[u][i];
        if (num[v] == UNVISITED) {
            cycleCheck(v, u);
            path.pop_back();
        }
        else if (num[v] == EXPLORED && v != par) {
            vi cycle;
            int i = path.size() - 1;
            while (true) {
                int n = path[i--];
                cycle.push_back(n);
                if (n == v) break;
            }
            vi temp = cycle;
            sort(temp.begin(), temp.end());
            if (dicc.count(temp) == 0) {
                dicc[temp] = C++;
                cycles.push_back(cycle);
            }
            path.pop_back();
        }
    }
    num[u] = VISITED;
}
예제 #25
0
void dfs(int u){
	vis[u] = 1;
	for (int i = 0; i < adj[u].size(); ++i) {
		if(!vis[adj[u][i]])dfs(adj[u][i]);
	}
	stk.push_back(u);
}
void dfsTS(int u) {
    visited[u] = VISITED;
    for(auto neighbor : graph[u]) {
        if(visited[neighbor] == UNVISITED) dfsTS(neighbor);
    }
    vqueue.push_back(u);
}
int getCenters(){
  
  queue<int> q;
  int maxlevel = 0;
  for(int i = 1; i <= n; i++){
    if(degree[i] == 1){
      q.push(i);
    }
  }
  
  while(!q.empty()){
    int u = q.front(); q.pop();
    for(int i = 0; i < (int) adj[u].size(); i++){
      int v = adj[u][i];
      if(--degree[v] == 1){
        q.push(v);
        level[v] = level[u] + 1;
        maxlevel = max(maxlevel, level[v]);
      }
    }
  }

  for(int i = 1; i <= n; i++){
    if(level[i] == maxlevel){
      centers.push_back(i);
    }
  }
  
  diameter = (maxlevel*2 + centers.size() - 1);

  return (diameter + 1)/2;
}
void djk(int a, int b, int rote,int pre){
    printf("%d -> %d > %d // cust = %d\n",pre,a,b,rote);
    if(matriz[a][b] < INF) {
        printf("%d -> %d > %d // cust = %d\n",pre,a,b,rote + matriz[a][]b);
        all.push_back(rote + matriz[a][b]);
        return;
    }
예제 #29
0
void eratosthenes(int n, vi &primes) {
	vb list(n + 1, true);
	primes.push_back(2);

	for (int p = 3; p * p <= n; p += 2) {
		if (list[p]) {
			for (ll pp = p * p; pp <= n; pp += 2 * p) {
				list[pp] = false;
			}
		}
	}

	for (int i = 3; i <= n; i += 2) {
		if (list[i]) primes.push_back(i);
	}
}
예제 #30
0
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;
		}
	}
}