Sunday, June 26, 2016

Published June 26, 2016 by with 0 comment

UVA 10815 Solution

#include<bits/stdc++.h>
using namespace std;

int main()
{
    set<string> ss;
    char line[205];
    while (gets(line))
    {
        int l=strlen(line);
        for (int i=0;i<l;i++)
        {
            if (line[i]>='A' && line[i]<='Z') line[i]+=32;
        }
        for (int i=0;i<l;i++)
        {
            int j=0;
            string s="";
            while (j<l && line[i]>='a' && line[i]<='z')
            {
                s+=line[i];
                i++;
                j=1;
            }
            if (j)
            {
                ss.insert(s);
                i--;
            }
        }
    }
    set<string> :: iterator it;
    for (it=ss.begin();it!=ss.end();it++) cout<<*it<<endl;
    return 0;
}
Read More
      edit
Published June 26, 2016 by with 0 comment

UVA 10954 Solution

#include<cstdio>
#include<queue>
using namespace std;

int main()
{
    int n;
    while (scanf("%d",&n)==1)
    {
        if (n==0) return 0;
        priority_queue<int > pq;
        while (n--)
        {
            int a;
            scanf("%d",&a);
            pq.push(-1*a);
        }
        long long int cost=0;
        while (pq.size()>1)
        {
            int a=pq.top();
            pq.pop();
            int b=pq.top();
            pq.pop();
            int c=a+b;
            cost+=c;
            pq.push(c);
        }
        printf("%lld\n",-1*cost);
    }
    return 0;
}
Read More
      edit
Published June 26, 2016 by with 0 comment

UVA 12646 Solution

#include<cstdio>
using namespace std;

int main()
{
    int a,b,c;
    while (scanf("%d%d%d",&a,&b,&c)!=EOF)
    {
        if (a==b && b==c) printf("*\n");
        else if (a==b) printf("C\n");
        else if (b==c) printf("A\n");
        else printf("B\n");
    }
    return 0;
}
Read More
      edit
Published June 26, 2016 by with 0 comment

UVA 11152 Solution

#include<cstdio>
#include<math.h>
using namespace std;

#define pi acos(-1)

int main()
{
    double a,b,c;
    while (scanf("%lf%lf%lf",&a,&b,&c)!=EOF)
    {
        double s=(a+b+c)/2.0;
        double triangle_area      =  sqrt(s*(s-a)*(s-b)*(s-c));
        double incircle_radius    =  triangle_area/s;
        double incircle_area      =  pi*incircle_radius*incircle_radius;
        double circumcircle_radius=a*b*c/sqrt((a+b+c)*(a+b-c)*(a-b+c)*(b+c-a));
        double circumcircle_area  =  pi*circumcircle_radius*circumcircle_radius;
        double sunflower = circumcircle_area-triangle_area;
        double violet = triangle_area-incircle_area;
        double red = incircle_area;
       
        printf("%.4lf %.4lf %.4lf\n",sunflower,violet,red);
    }
    return 0;
}
Read More
      edit
Published June 26, 2016 by with 0 comment

UVA 12704 Solution

#include<cstdio>
#include<math.h>
using namespace std;

int main()
{
    int tc;
    scanf("%d",&tc);
    while (tc--)
    {
        double x,y,r,dis;
        scanf("%lf%lf%lf",&x,&y,&r);
        dis=sqrt(x*x+y*y);
        printf("%.2lf %.2lf\n",r-dis,r+dis);
    }
    return 0;
}
Read More
      edit
Published June 26, 2016 by with 0 comment

UVA 12917 Solution

#include<cstdio>
using namespace std;

int main()
{
    int h,p,o;
    while (scanf("%d%d%d",&p,&h,&o)!=EOF)
    {
        if (h+p>o) printf("Hunters win!\n");
        else printf("Props win!\n");
    }
    return 0;
}
Read More
      edit
Published June 26, 2016 by with 0 comment

UVA 673 Solution

#include<cstdio>
#include<stack>
#include<string.h>
using namespace std;

int main()
{
    int n;
    scanf("%d",&n);
    getchar();
    while (n--)
    {
        stack<char> check;
        char s[130];
        gets(s);
        int l=strlen(s);
        for (int i=0;i<l;i++)
        {
            if (s[i]=='[' || s[i]=='(')
                check.push(s[i]);
            else if (s[i]==']')
            {
                if (check.empty() || check.top()!='[')
                {
                    check.push('s');
                    break;
                }
                else check.pop();
            }
            else if (s[i]==')')
            {
                if (check.empty() || check.top()!='(')
                {
                    check.push('s');
                    break;
                }
                else check.pop();
            }
        }
        if (check.empty()) printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}
Read More
      edit

Saturday, June 25, 2016

Published June 25, 2016 by with 0 comment

UVA 10608 Solution

#include<cstdio>
#include<cstring>
using namespace std;

int parent[30001];
int level[30001];

int findparent(int n)
{
    return parent[n]==n? n: findparent(parent[n]);
}

int main()
{
    int n,m,tc,a,b;
    scanf("%d",&tc);
    while (tc--)
    {
        scanf("%d%d",&n,&m);
        if (m+n==0) return 0;
        n++;
        for (int i=1;i<n;i++)
        {
            parent[i]=i;
            level[i]=1;
        }
        while (m--)
        {
            scanf("%d%d",&a,&b);
            a=findparent(a);
            b=findparent(b);
            if (a!=b)
            {
                parent[a]=b;
                level[b]+=level[a];
            }
        }
        bool visited[n];
        memset(visited,false,sizeof(visited));
        int max=0;
        for (int  i=1;i<n;i++)
        {
            if (max<level[i]) max=level[i];
        }
        printf("%d\n",max);
       
    }
   
    return 0;
}
Read More
      edit
Published June 25, 2016 by with 0 comment

UVA 10583 Solution

#include<cstdio>
using namespace std;

int parent[50001];

int findparent(int n)
{
    return parent[n]==n? n: findparent(parent[n]);
}

int main()
{
    int n,m,tc=1,a,b;
    while (scanf("%d%d",&n,&m)!=EOF)
    {
        if (m+n==0) return 0;
        for (int i=1;i<=n;i++) parent[i]=i;
        while (m--)
        {
            scanf("%d%d",&a,&b);
            parent[findparent(a)]=findparent(b);
        }
        int r=0;
        for (int i=1;i<=n;i++)
            if (parent[i]==i) r++;
       
        printf("Case %d: %d\n",tc++,r);
       
    }
   
    return 0;
}
Read More
      edit

Tuesday, June 21, 2016

Published June 21, 2016 by with 0 comment

UVA 11679 solution

#include<cstdio>
#include<cstring>
using namespace std;

int main()
{
    int b,n,d,c,v,reserve[25],debit[25],credit[25];
    while (scanf("%d%d",&b,&n)==2)
    {
        if (!b && !n) return 0;
        memset(reserve,0,sizeof(reserve));
        memset(debit,0,sizeof(debit));
        memset(credit,0,sizeof(credit));
        for (int i=1;i<=b;i++) scanf("%d",&reserve[i]);
        for (int i=0;i<n;i++)
        {
            scanf("%d%d%d",&d,&c,&v);
            debit[d]+=v;
            credit[c]+=v;
        }
        int flag=1;
        for (int i=1;i<=b;i++)
        {
            if (credit[i]+reserve[i]-debit[i]<0)
            {
                flag=0;
                break;
            }
        }
        if (flag) printf("S\n");
        else printf("N\n");
    }
    return 0;
}
Read More
      edit

Sunday, June 19, 2016

Published June 19, 2016 by with 0 comment

uva 11428 solution

#include<cstdio>
#include<cmath>
using namespace std;

int main()
{
    int n,cube[70];
    for (int i=0;i<60;i++) cube[i]=i*i*i;
    while (scanf("%d",&n)!=EOF)
    {
        if (n==0) break;
        int i,j,flag=0;
        for (i=0;i<59;i++)
        {
            for (j=i+1;j<60;j++)
                if (cube[j]-cube[i]==n)
                {
                    flag=1;
                    goto print;
                }
        }
        print:
            if (flag) printf("%d %d\n",j,i);
            else printf("No solution\n");
    }
    return 0;
}
Read More
      edit

Saturday, June 18, 2016

Published June 18, 2016 by with 0 comment

uva 12952 solution

#include<algorithm>
#include<cstdio>
using namespace std;

int main()
{
    int a,b;
    while (scanf("%d%d",&a,&b)!=EOF)
        printf("%d\n",max(a,b));
    return 0;
}
Read More
      edit

Sunday, June 12, 2016

Published June 12, 2016 by with 0 comment

uva :336 - A Node Too Far

#include<cstdio>
#include<vector>
#include<queue>
#include<map>
using namespace std;

map<int,bool>visit;

int bfs(map<int , vector <int > >graph, int source, int ttl)
{
    int count=1,f,node,size;
    queue<int > q;
    map<int,int>level;
    level[source]=ttl;
    visit[source]=true;
    q.push(source);
    while (!q.empty())
    {
        f=q.front();
        q.pop();
        if (level[f]==0) break;
        size=graph[f].size();
        for (int i=0;i<size;i++)
        {
            node=graph[f][i];
            if (visit[node]==false)
            {
                q.push(node);
                visit[node]=true;
                level[node]=level[f]-1;
                count++;
            }
        }
    }
    visit.clear();
    return count;
   
}

int main()
{
    int node,edge,case_no=1,a,b,source,ttl;
    while (scanf("%d",&edge) && edge)
    {
        map <int,vector <int > >graph;
        for (int i=0;i<edge;i++)
        {
            scanf("%d%d",&a,&b);
            graph[a].push_back(b);
            graph[b].push_back(a);
            visit[a]=false;
            visit[b]=false;
        }
       
        while (scanf("%d%d",&source,&ttl)==2)
        {
            if (source==0 && ttl==0) break;
            int visited=bfs(graph,source,ttl);
            printf("Case %d: %d nodes not reachable from node %d with TTL = %d.\n",case_no++,graph.size()-visited,source,ttl);
        }
       
    }
    return 0;
}
Read More
      edit

Friday, June 10, 2016

Published June 10, 2016 by with 0 comment

UVA 11340 solution

#include<cstdio>
#include<string.h>
using namespace std;

int main()
{
    int t;
    scanf("%d",&t);
    while (t--)
    {
        int n,m,v[111],total=0;
        char c[111],s[10005];
        scanf("%d",&n);
        for (int i=0;i<n;i++)
        {
            getchar();
            scanf("%c%d",&c[i],&v[i]);
        }
        scanf("%d",&m);
        getchar();
        while (m--)
        {
            gets(s);
            int l=strlen(s);
            for (int j=0;j<n;j++)
                for (int i=0;i<l;i++)
                    if (c[j]==s[i]) total+=v[j];
        }
        double ans=total/100.0;
        printf ("%.2lf$\n",ans );
    }
    return 0;
}
Read More
      edit

Thursday, June 9, 2016

Published June 09, 2016 by with 0 comment

uva 11995 solution

#include<cstdio>
#include<stack>
#include<queue>
#include<algorithm>
#include<stdlib.h>
using namespace std;

int main()
{
    int n;
    while (scanf("%d",&n)!=EOF)
    {
        stack<int>s;
        queue<int>q;
        priority_queue<int>pq;
        int stk=1,que=1,pque=1,a,b;
        while (n--)
        {
            scanf("%d%d",&a,&b);
            if (a==1)
            {
                if (stk) s.push(b);
                if (que) q.push(b);
                if (pque) pq.push(b);
            }
            else
            {
                if (stk)
                {
                    if (s.empty() || s.top()!=b) stk=0;
                    else s.pop();
                }
                if (que)
                {
                    if (q.empty() || q.front()!=b) que=0;
                    else q.pop();
                }
                if (pque)
                {
                    if (pq.empty() || pq.top()!=b) pque=0;
                    else pq.pop();
                }
            }
        }
        if (stk+que+pque==0) printf("impossible\n");
        else if (stk+que+pque==1)
        {
            if (stk) printf("stack\n");
            else if (que) printf("queue\n");
            else printf("priority queue\n");
        }
        else if (stk+que+pque>1) printf("not sure\n");
    }
    return 0;
}
Read More
      edit