Sunday, December 25, 2016

UVA 336 - A Node Too Far

#include <bits/stdc++.h>
#define sf scanf
#define pf printf
using namespace std;
map<int, int>visited;

void bfs(int start, map<int, vector<int> > graph)
{
queue<int>q;
q.push(start);
visited[start] = 0;
while(!q.empty()){
int top = q.front();
q.pop();
int size = graph[top].size();
for(int i=0; i<size; i++){
int n = graph[top][i];
if(!visited.count(n)){
visited[n]=visited[top]+1;
q.push(n);
}
}
}

}

int main()
{
ios_base::sync_with_stdio(0);
int nodes , a  , b , kase =0;
while(cin >> nodes && nodes){
map<int, vector<int> >graph;
for(int i=0; i<nodes; i++){
cin >> a >> b;
graph[a].push_back(b);
graph[b].push_back(a);
}
int ttl , start;
while(cin >> start >> ttl ){
if(start==0 && ttl ==0)break;
map<int, int>::const_iterator it;
visited.clear();
bfs(start , graph);
int cnt =0;
for(it = visited.begin(); it!=visited.end(); it++){
if((*it).second >ttl)cnt++;
}
cnt += graph.size()-visited.size();

cout << "Case "<<++kase << ": "<<cnt <<" nodes not reachable from node "<<start<<" with TTL = "<<ttl<<".\n";

}
}
}

Wednesday, December 14, 2016

UVA 280 - Vertex

#include<bits/stdc++.h>
using namespace std;
vector<int>adj[105];
bool vis[105];
void bfs(int s , int nodes)
{
    fill(vis,vis+nodes,false);
    queue<int>q;
    while(!q.empty())q.pop();
    q.push(s);
    while(!q.empty()){
        int fr = q.front();
        q.pop();
        for(int i = 0; i < adj[fr].size(); i++){
            if(!vis[adj[fr][i]]){
                vis[adj[fr][i]] = true;
                q.push(adj[fr][i]);
            }

        }

    }
    int cnt = 0;
    for(int i=0; i<nodes; i++){
        if(!vis[i])cnt++;
    }
    cout <<cnt;
    for(int i=0; i<nodes; i++){
        if(!vis[i])cout <<' '<<i+1;
    }
    cout << '\n';

}

int main()
{
    int nodes;
    while(cin >> nodes && nodes){

        memset(adj,0,sizeof adj);

        int i;
        while(cin >> i && i){
            int j;
            while( cin >> j && j){
                adj[i-1].push_back(j-1);
            }
        }
        int t;
        cin >> t;
        while(t--){
            int query;
            cin >> query;
            bfs(query-1 , nodes);
        }

    }

return 0;
}

Saturday, November 5, 2016

UVA 11371 - Number Theory for Newbies

#include<bits/stdc++.h>
#define sf scanf
#define pf printf
#define LL long long
#define REP(i,MAX) for(LL i = 0; i<MAX; i++)
#define MAX 10000000+10
using namespace std;
int main()
{
    char s[100];
    while(cin >> s){
    sort(s, s+strlen(s));
    for (int i = 0; i < strlen(s); ++i)
    {
    if(s[i] != '0'){
    swap(s[0],s[i]);
    break;
    }
    }
    long long a , b;
    sscanf(s,"%lld",&b);
    sort(s, s+strlen(s));
    for (int i = 0 , j =strlen(s)-1; i < j; ++i,j--)
    {
    swap(s[i],s[j]);

    }
    sscanf(s,"%lld",&a);
    cout<<a<<" - "<<b<<" = "<<a-b<<" = 9 * "<<(a-b)/9<<'\n';
    }

 return 0;
}

UVA 10110 - Light, more light

#include<bits/stdc++.h>
#define sf scanf
#define pf printf
#define LL long long
#define REP(i,MAX) for(LL i = 0; i<MAX; i++)
#define MAX 10000000+10
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    long long n;
    while(cin >> n && n){
    floor(sqrt(n)) == ceil(sqrt(n)) ? cout<<"yes\n":cout <<"no\n";
    }

 return 0;
}

Friday, November 4, 2016

UVA 10168 - Summation of Four Primes

#include<bits/stdc++.h>
#define sf scanf
#define pf printf
#define LL long long
#define REP(i,MAX) for(LL i = 0; i<MAX; i++)
#define MAX 10000000+10
using namespace std;
bool prime[MAX];
vector<LL>P;
void init_prime() {
  prime[2] = true;
  for (LL i = 3; i < MAX; i += 2) prime[i] = true;

  for (LL i = 3; i*i < MAX; i += 2)
    if (prime[i])
      for (LL j = i*i; j < MAX; j += i+i)
        prime[j] = false;
}

int main()
{
    ios_base::sync_with_stdio(false);
    init_prime();
   LL n;
   while(cin >> n){
    bool found = false;
    if(n < 8){
        cout << "Impossible.\n";
        continue;
    }
    if(n%2 != 0){
        cout <<"2 3 ";
        n-=5;
    }
    else{
        cout <<"2 2 ";
        n-=4;
    }
    if(n == 4){
        cout << "2 2 ";
    }
    else{
        for (int i = 3; i <=(n/2) ; i+=2)
        {
            if(prime[i] && prime[n-i]){
                cout << i <<' '<<n-i<<'\n';
                found = true;
                break;
            }
        }
    }
    if(!found) cout << "Impossible.\n";
   }

 return 0;
}

Thursday, November 3, 2016

UVA 10948 - The primary problem

#include<bits/stdc++.h>
#define sf scanf
#define pf printf
#define LL long long
#define REP(i,MAX) for(int i = 0; i<MAX; i++)
#define MAX 1000010
bool prime[MAX];
using namespace std;
void init_prime() {
  prime[2] = true;
  for (int i = 3; i < MAX; i += 2) prime[i] = true;

  for (int i = 3; i*i < MAX; i += 2)
    if (prime[i])
      for (int j = i*i; j < MAX; j += i+i)
        prime[j] = false;
}

int main()
{
    ios_base::sync_with_stdio(false);
    init_prime();
    int n;
    while(cin >> n && n){
        cout << n << ":\n";
        bool found = false;
        int i;
        for (i = 2; i <= n; ++i)
        {
            if(i > n-i)break;
            if(prime[i] && prime[n-i]){
                found = true;
                break;
            }
        }
        if(found)cout <<i<<'+'<<n-i<<'\n';
        else cout <<"NO WAY!\n";
    }


 return 0;
}

Tuesday, October 25, 2016

UVA 136 - Ugly Numbers

#include<bits/stdc++.h>
#define sf scanf
#define pf printf
#define LL long long
#define REP(i,MAX) for(int i = 0; i<MAX; i++)
#define MAX INT_MAX
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    puts("The 1500'th ugly number is 859963392.");

 return 0;
}