int main(){ int n, m; while(true){ cin >> n >> m; if(n == 0 && m ==0) break; adj.assign(n,vi()); visitados.assign(n,0); s.clear(); for(int k = 0; k < m; k++){ int principal, dependente; cin >> principal >> dependente; (adj.at(principal-1)).push_back(dependente-1); } for(int l = 0; l < n; l++){ if(visitados[l] == 0){ dfs(l); } } string result; for(int d = s.size()- 1 ; d > 0;d--){ stringstream ss; ss << s.at(d); result += ss.str() + " "; } stringstream ss; ss << s.at(0); result += ss.str(); cout << result << endl; } }
int main(int argc, char** argv) { cin.sync_with_stdio(false); sieve(1000000); //sieve(10000000); int L, U; while (cin >> L >> U) { //cout << L << " " << U << endl; int min_dist = 1000000; int max_dist = -1; pair<int, int> closest; pair<int, int> furthest; for (int i = L; i <= U; ++i) // find all primes within range { if (is_prime(i)) { _primes.push_back(i); } } if (_primes.size() < 2) { cout << "There are no adjacent primes.\n"; } else { for (int i = 0; i < _primes.size()-1; ++i) // iterate through all primes { int dist = _primes.at(i+1) - _primes.at(i); //cout << "dist " << _primes[i] << " to " << _primes[i+1] << " = " << dist << endl; if (dist < min_dist) { closest.first = _primes[i]; closest.second = _primes[i+1]; min_dist = dist; } if (dist > max_dist) { furthest.first = _primes[i]; furthest.second = _primes[i+1]; max_dist = dist; } //cout << _primes[i] << " "; } cout << closest.first << "," << closest.second << " are closest, " << furthest.first << "," << furthest.second << " are most distant.\n"; } _primes.clear(); } return 0; }
void dfs(int u){ if(visitados.at(u) == 0){ //printf("dfs em %d\n", u); visitados[u] = 1; for(int i = 0; i < adj.at(u).size(); i++){ int vizinho = adj.at(u)[i]; if(!visitados[vizinho]){ dfs(vizinho); } } //printf("adicionando %d ao stack\n", u+1); s.push_back(u+1); } }