Merge pull request #5 from Kiritow/master

Catch up with you.
This commit is contained in:
KiritoTRw 2016-05-01 14:27:27 +08:00
commit 16fb1a3072
11 changed files with 694 additions and 0 deletions

70
NYOJ/207.cpp Normal file
View File

@ -0,0 +1,70 @@
#include <bits/stdc++.h>
using namespace std;
#define MAXN 50005
int father[MAXN];
///Relationship: 0 Same 1 Father--eat-->son 2 son--eat-->father
int relation[MAXN];
int find(int pos)
{
if(father[pos]!=pos)
{
int x=father[pos];
father[pos]=find(x);
relation[pos]=(relation[pos]+relation[x])%3;
}
return father[pos];
}
int main()
{
int N,K;
while(scanf("%d %d",&N,&K)==2)
{
/// Init
for(int i=0; i<=N; i++)
{
relation[i]=0;
father[i]=i;
}
int a,b,c;
int cnt=0;
for(int i=0;i<K;i++)
{
scanf("%d %d %d",&a,&b,&c);
if(a==2&&b==c)
{
++cnt;continue;
}
if(b>N||c>N)
{
++cnt;continue;
}
int rootfather_b=find(b);
int rootfather_c=find(c);
if(rootfather_b==rootfather_c)
{
if(a==1&&relation[b]!=relation[c])
{
++cnt;continue;
}
if(a==2&&relation[b]!=(relation[c]+2)%3)
{
++cnt;continue;
}
}
else
{
father[rootfather_c]=rootfather_b;
relation[rootfather_c]=(relation[b] + (a-1) + (3-relation[c])) % 3;
}
}
printf("%d\n",cnt);
}
return 0;
}

81
NYOJ/207_cnblogs.cpp Normal file
View File

@ -0,0 +1,81 @@
#include <cstdio>
#include <iostream>
using namespace std;
const int N = 50005;
int father[N];
int relation[N];//根点节到点节的关系
void init(int n)
{
for(int i = 0; i <= n; ++i)
{
father[i]= i;
relation[i] = 0;
}
}
//更新的步调,先将当前点节与其根点节相连,然后更新其与根点节的关系
//当前节点x与根节点r的关系更新的方法
// (x与其父点节的关系+其父点节的关系与根点节的关系)%3
//所以在更新节点x的数据之前需要更新其父节点的数据这是find为什么搞成递归函数的原因
//其更新的次序是从根节点开始往下始终到当前点节x的父点节。
int find(int x)
{
if(x != father[x])//不是根点节
{
int temp = father[x];
//将当前点节的父点节设置为根点节
father[x] = find(temp);
//更新当前点节与根点节的关系由x->x父和x父->父根的关系失掉x->父根的关系
//所以在这之前必须更新其父点节与根点节的关系
relation[x] = (relation[x] + relation[temp]) % 3;
}
return father[x];
}
int main()
{
int n, m, x, y, d, fx, fy, cnt;
while(~scanf("%d %d", &n, &m))//POJ上只要需一次入输所以不要需while循环
{
cnt = 0;
init(n);
for(int i = 0; i < m; ++i)
{
scanf("%d %d %d", &d, &x, &y);
if(x > n || y > n)
{
++cnt;
continue;
}
if(d == 2 && x == y)
{
++cnt;
continue;
}
fx = find(x);
fy = find(y);
if(fx == fy)//属于同一个子集
{
//如果x、y是同类那么他们相对根点节的关系应该是一样的
if(d == 1 && relation[x] != relation[y])
++cnt;
//如果不是同类加入x与y的关系之后x相对根点节的关系(x根->y,y->x(即3-(d-1)=2).即x根->x)应该是不变的
//这里d=2表示x - y = 2-1=1;而y->x=3-(x->y)=3-1=2;
if(d == 2 && relation[x] != (relation[y] + 2)%3)
++cnt;
}
else//合并两个连通区域
{
father[fy] = fx;//y根的父点节更新成x根
//(d-1)为x与y的关系3-relation[y]是y与y的根点节的关系注意方向relation[x]是其根点节与x的关系
//x根->x,x->y,y->y根即x根->y根
relation[fy] = (relation[x] + (d-1) + (3-relation[y])) % 3;//注意这里只更新的是fy相对于根的关系
}
}
printf("%d\n", cnt);
}
return 0;
}

75
NYOJ/207_csdn.cpp Normal file
View File

@ -0,0 +1,75 @@
#include<stdio.h>
const int N = 50005;
int parent[N];
int relation[N];//根节点到节点的关系
void Init(int n)
{
for(int i = 0; i <= n; ++i)
{
parent[i]= i;
relation[i] = 0;
}
}
//更新的步骤,先将当前节点与其根节点相连,然后更新其与根节点的关系
//当前节点x与根节点r的关系更新的方式(x与其父节点的关系+其父节点的关系与根节点的关系)%3
//所以在更新节点x的数据之前需要更新其父节点的数据这是Find为什么搞成递归函数的原因
//其更新的顺序是从根节点一直往下一直到当前节点x的父节点。
int Find(int x)
{
if(x != parent[x])//不是根节点
{
int temp = parent[x];
//将当前节点的父节点设置为根节点
parent[x] = Find(temp);
//更新当前节点与根节点的关系由x->x父和x父->父根的关系得到x->父根的关系
//所以在这之前必须更新其父节点与根节点的关系
relation[x] = (relation[x] + relation[temp]) % 3;
}
return parent[x];
}
int main()
{
int n,m,i;
int x,y,d;
int rx,ry;
int cnt;
while(scanf("%d %d", &n, &m) != EOF)//POJ上只需要一次输入所以不需要while循环
{
cnt = 0;
Init(n);
for(i = 0; i < m; ++i)
{
scanf("%d %d %d", &d, &x, &y);
if(x > n || y > n)
{
++cnt;
continue;
}
if(d == 2 && x == y)
{
++cnt;
continue;
}
rx = Find(x);
ry = Find(y);
if(rx == ry)//属于同一个子集
{
//如果x、y是同类那么他们相对于根节点的关系应该是一样的
//如果不是同类加入y之后x相对于根节点的关系(x根->y,y->x(即3-(d-1)=2).即x根->x)应该是不变的
if((d == 1 && relation[x] != relation[y]) ||
(d == 2 && relation[x] != (relation[y] + 2)%3))
++cnt;
}
else//合并两个连通区域
{
parent[ry] = rx;//y根的父节点更新成x根
//(d - 1)为x与y的关系3-relation[y]是y与y的根节点的关系注意方向relation[x]是其根节点与x的关系
//x根->x,x->y,y->y根即x根->y根
relation[ry] = (relation[x] + d - 1 + 3 - relation[y]) % 3;
}
}
printf("%d\n", cnt);
}
return 0;
}

61
NYOJ/207_iphxer.cpp Normal file
View File

@ -0,0 +1,61 @@
#include<iostream>
#include<hash_map>
//#include"iphxer_h.h"
#include<string>
#include<cstring>
#include<cstdio>
#include<map>
using namespace std;
#define maxsize 50100
int rank1[maxsize];//相对于根元素的权值0同类1x吃father[x],2表示被father[x] 吃
int father[maxsize];
void initU(int n){
for(int i=0;i<n;i++){
father[i]=i;
rank1[i]=0;
}
}
int Find(int x){
if(father[x]==x)return x;
int t=father[x];//保存更新前的father位置用来更新子rank
father[x]=Find(father[x]);
rank1[x]=(rank1[t]+rank1[x])%3;//计算相对于根节点的rank值
return father[x];
}
//把y合并到x中关系为d
void Union(int x,int y,int d)
{
int xf=Find(x);
int yf=Find(y);
father[xf]=yf;//合并到y上
rank1[xf]=(rank1[y]-rank1[x]+3+d)%3;
}
int main()
{
int i,n,m;
int d,x,y,xf,yf;
int sum=0;
scanf("%d%d",&n,&m);
initU(n);
for(int i=0;i<m;i++){
scanf("%d%d%d",&d,&x,&y);
if(x>n||y>n||(d==2&&x==y))
++sum;
else{
xf=Find(x);
yf=Find(y);
if(xf==yf){
if((rank1[x]-rank1[y]+3)%3!=d-1)
sum++;
}else{
Union(x,y,d-1);
}
}
}
cout<<sum<<endl;
}

47
NYOJ/208.cpp Normal file
View File

@ -0,0 +1,47 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAXN 10005
struct pack
{
int profit,deadline;
};
pack p[MAXN];
bool cmp(const pack& a,const pack& b)
{
return a.profit>b.profit;
}
bool isdayused[MAXN];
int main()
{
int t;
while(scanf("%d",&t)==1)
{
memset(p,0,sizeof(pack)*MAXN);
memset(isdayused,false,sizeof(bool)*MAXN);
for(int i=0;i<t;i++)
{
scanf("%d %d",&p[i].profit,&p[i].deadline);
}
sort(p,p+t,cmp);
int sum=0;
for(int i=0;i<t;i++)
{
for(int day=p[i].deadline;day>0;day--)
{
if(!isdayused[day])
{
isdayused[day]=true;
sum+=p[i].profit;
break;
}
}
}
printf("%d\n",sum);
}
return 0;
}

68
NYOJ/208_2.cpp Normal file
View File

@ -0,0 +1,68 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAXN 10005
struct pack
{
int profit,deadline;
};
pack p[MAXN];
int father[MAXN];
bool cmp(const pack& a,const pack& b)
{
return a.profit>b.profit;
}
int GetFather(int pos)
{
if(father[pos]==-1)
{
/// This pos is itself's father
return pos;
}
/// Else , this pos has a father. So it should know who is his Eldest Father.
father[pos]=GetFather(father[pos]);
/// And then , report back to it's caller.
return father[pos];
}
int main()
{
int t;
while(scanf("%d",&t)==1)
{
/// Reset father
/** This way caused TLE.
for(int i=0;i<t;i++)
{
father[i]=i;
}
*/
memset(father,-1,sizeof(int)*MAXN);
for(int i=0;i<t;i++)
{
scanf("%d %d",&p[i].profit,&p[i].deadline);
}
sort(p,p+t,cmp);
int sum=0;
for(int i=0;i<t;i++)
{
int ans=GetFather(p[i].deadline);
if(ans>0)
{
/// This pos's father is not 0 : which means it's possible to sell this thing.
sum+=p[i].profit;
/// And This pos become a son of the pos on its left.
father[ans]=GetFather(ans-1);
}
}
printf("%d\n",sum);
}
return 0;
}

38
NYOJ/208_csdn.cpp Normal file
View File

@ -0,0 +1,38 @@
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
const int N = 10005;
int arr[N][2];
bool used[N];//表示第几天被占用了
int compare(const void *a, const void *b)
{
int *p1 = (int *)a;
int *p2 = (int *)b;
return p2[0] - p1[0];//价格降序
}
int main()
{
int n,i,j,sum,d;
while(scanf("%d", &n) != EOF)
{
memset(used, 0, sizeof(used));
for(i = 0; i < n; ++i)
scanf("%d %d", &arr[i][0], &arr[i][1]);
qsort(arr, n, 2*sizeof(int), compare);
sum = 0;
for(i = 0; i < n; ++i)
{
for(j = arr[i][1]; j > 0; --j)
{
if(!used[j])//在这之前,有日期可用
{
used[j] = true;
sum += arr[i][0];
break;
}
}
}
printf("%d\n", sum);
}
return 0;
}

70
POJ/1182.cpp Normal file
View File

@ -0,0 +1,70 @@
#include <cstdio>
using namespace std;
#define MAXN 50005
int father[MAXN];
///Relationship: 0 Same 1 Father--eat-->son 2 son--eat-->father
int relation[MAXN];
int find(int pos)
{
if(father[pos]!=pos)
{
int x=father[pos];
father[pos]=find(x);
relation[pos]=(relation[pos]+relation[x])%3;
}
return father[pos];
}
int main()
{
int N,K;
scanf("%d %d",&N,&K);
{
/// Init
for(int i=0; i<=N; i++)
{
relation[i]=0;
father[i]=i;
}
int a,b,c;
int cnt=0;
for(int i=0;i<K;i++)
{
scanf("%d %d %d",&a,&b,&c);
if(a==2&&b==c)
{
++cnt;continue;
}
if(b>N||c>N)
{
++cnt;continue;
}
int rootfather_b=find(b);
int rootfather_c=find(c);
if(rootfather_b==rootfather_c)
{
if(a==1&&relation[b]!=relation[c])
{
++cnt;continue;
}
if(a==2&&relation[b]!=(relation[c]+2)%3)
{
++cnt;continue;
}
}
else
{
father[rootfather_c]=rootfather_b;
relation[rootfather_c]=(relation[b] + (a-1) + (3-relation[c])) % 3;
}
}
printf("%d\n",cnt);
}
return 0;
}

44
POJ/2386.cpp Normal file
View File

@ -0,0 +1,44 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
char map[105][105];
void dfs(int line,int col)
{
if(map[line][col]=='.'||map[line][col]==0) return;
map[line][col]='.';
dfs(line-1,col-1);
dfs(line-1,col);
dfs(line-1,col+1);
dfs(line,col-1);
dfs(line,col+1);
dfs(line+1,col-1);
dfs(line+1,col);
dfs(line+1,col+1);
}
int main()
{
int n,m;
scanf("%d %d",&n,&m);
for(int i=1;i<n+1;i++)
{
scanf("%s",&map[i][1]);
}
int cnt=0;
for(int i=1;i<n+1;i++)
{
for(int j=1;j<m+1;j++)
{
if(map[i][j]=='W')
{
dfs(i,j);
cnt++;
}
}
}
printf("%d\n",cnt);
return 0;
}

75
POJ/3278.cpp Normal file
View File

@ -0,0 +1,75 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <stack>
using namespace std;
struct treenode
{
int value;
treenode *plus,*reduce,*add;
};
typedef struct treenode tree;
tree* root=NULL;
stack<tree*> bus;
stack<tree*> tmpbus;
stack<tree*> answerbus;
bool vis[100001];
int main()
{
int n,k;
scanf("%d %d",&n,&k);
root=new (nothrow) tree;
root->value=n;
bus.push(root);
int currentstep=-1;
while(answerbus.empty())
{
++currentstep;
while(!bus.empty())
{
tree* father=bus.top();
vis[father->value]=true;
if(father->value==k)
{
answerbus.push(father);
break;
}
bus.pop();
int vplus=father->value*2,vreduce=father->value-1,vadd=father->value+1;
if(vplus<100001&&!vis[vplus])
{
tree* node=new (nothrow) tree;
node->value=vplus;
father->plus=node;
tmpbus.push(node);
}
if(vadd<100001&&!vis[vadd])
{
tree* node=new (nothrow) tree;
node->value=vadd;
father->add=node;
tmpbus.push(node);
}
if(vreduce>-1&&!vis[vreduce])
{
tree* node=new (nothrow) tree;
node->value=vreduce;
father->reduce=node;
tmpbus.push(node);
}
}
while(!tmpbus.empty())
{
bus.push(tmpbus.top());
tmpbus.pop();
}
}
printf("%d\n",currentstep);
return 0;
}

65
SDUTOJ/3251.cpp Normal file
View File

@ -0,0 +1,65 @@
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <algorithm>
using namespace std;
struct STX
{
double height,weight;
};
bool comp(const STX& a,const STX& b)
{
return a.height<b.height;
}
int main()
{
int t;
scanf("%d",&t);
for(int tt=0;tt<t;tt++)
{
int n;
scanf("%d",&n);
vector<STX> vec;
for(int i=0;i<n;i++)
{
STX s;
scanf("%lf %lf",&s.height,&s.weight);
vec.push_back(s);
}
sort(vec.begin(),vec.end(),comp);
double red=0;
double blue=0;
for(int i=0;i<n;i++)
{
//printf("## vec.at(i).height= %f\n",vec.at(i).height);
if((i+1)%2)
{
//printf("## red\n");
red+=vec.at(i).weight;
}
else
{
//printf("## blue\n");
blue+=vec.at(i).weight;
}
}
if(blue>red)
{
printf("blue\n");
}
else if(blue<red)
{
printf("red\n");
}
else
{
printf("fair\n");
}
}
return 0;
}