Powered By HC TECH : AutoACer Engine

3000-3099
This commit is contained in:
KiritoTRw 2016-08-30 22:34:56 +08:00 committed by GitHub
parent 46c0830804
commit 7a5dfb949b
64 changed files with 4967 additions and 0 deletions

60
HDOJ/3001_autoAC.cpp Normal file
View File

@ -0,0 +1,60 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f
int State[12];
int visited[60000][12];
int dp[60000][12];
int map[12][12];
int n,m;
void Initiate()
{
State[0]=1;
for(int i=1;i<=10;i++)
State[i]=State[i-1]*3;
for(int i=0;i<=State[10];i++){
int tmp=i;
for(int j=0;j<=10;j++){
visited[i][j]=tmp%3;
tmp/=3;
}
}
}
int main()
{
Initiate();
int u,v,w,ans;
while(~scanf("%d%d",&n,&m)){
memset(dp,inf,sizeof(dp));
memset(map,inf,sizeof(map));
for(int i=0;i<n;i++)dp[State[i]][i]=0;
while(m--){
scanf("%d%d%d",&u,&v,&w);
u--,v--;
map[u][v]=map[v][u]=min(map[u][v],w);
}
ans=inf;
for(int i=0;i<State[n];i++){
bool flag=true;
for(int j=0;j<n;j++){
if(visited[i][j]==0)flag=false;
if(dp[i][j]==inf)continue;
for(int k=0;k<n;k++)if(j!=k){
if(visited[i][k]>=2)continue;
if(map[j][k]==inf)continue;
dp[i+State[k]][k]=min(dp[i+State[k]][k],dp[i][j]+map[j][k]);
}
}
if(flag){
for(int j=0;j<n;j++){
ans=min(ans,dp[i][j]);
}
}
}
if(ans==inf)ans=-1;
printf("%d\n",ans);
}
return 0;
}

75
HDOJ/3002_autoAC.cpp Normal file
View File

@ -0,0 +1,75 @@
#include<stdio.h>
#include<string.h>
const int N=105;
const int INF=1<<30;
int map[N][N];
int wet[N];
int vis[N];
int combine[N];
int S,T,mincut,n;
void search()
{
int i,j,max,tmp;
memset(vis,0,sizeof(vis));
memset(wet,0,sizeof(wet));
S=T=-1;
for(i=0;i<n;i++)
{
max=-INF;
for(j=0;j<n;j++)
{
if(!combine[j]&&!vis[j]&&wet[j]>max)
{
tmp=j;
max=wet[j];
}
}
if(T==tmp) return;
S=T;T=tmp;
mincut=max;
vis[tmp]=1;
for(j=0;j<n;j++)
{
if(!combine[j]&&!vis[j])
wet[j]+=map[tmp][j];
}
}
}
int Stoer_Wagner()
{
int i,j;
memset(combine,0,sizeof(combine));
int ans=INF;
for(i=0;i<n-1;i++)
{
search();
if(mincut<ans) ans=mincut;
if(ans==0) return 0; //杩
combine[T]=1;
for(j=0;j<n;j++)
{
if(!combine[j])
{
map[S][j]+=map[T][j];
map[j][S]+=map[j][T];
}
}
}
return ans;
}
int main()
{
int a,b,c,m,i;
while(~scanf("%d%d",&n,&m))
{
memset(map,0,sizeof(map));
for(i=0;i<m;i++)
{
scanf("%d%d%d",&a,&b,&c);
map[a][b]+=c;
map[b][a]+=c;
}
printf("%d\n",Stoer_Wagner());
}
return 0;
}

45
HDOJ/3003_autoAC.cpp Normal file
View File

@ -0,0 +1,45 @@
#include<iostream>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
#include<set>
using namespace std;
__int64 cal(__int64 n)
{
__int64 t=1;
__int64 ans=2;
__int64 m=n+1;
while(n!=1)
{
if(n&1)
{
t=t*ans;
n--;
t%=m;
}
else
{
ans*=ans;
n=n/2;
ans%=m;
}
}
return t*ans%m;
}
int main()
{
__int64 n;
while(scanf("%I64d",&n) && n)
{
if(n==1)
{
printf("0\n");
continue;
}
__int64 ans;
ans=cal(n-1);
printf("%I64d\n",(ans+1)%n);
}
return 0;
}

129
HDOJ/3004_autoAC.cpp Normal file
View File

@ -0,0 +1,129 @@
#include <iostream>
#include <queue>
#include <string.h>
using namespace std;
const int size = 11;
struct node
{
int mx, my;
int px, py;
int cx, cy;
int step;
};
int n, m;
int sx, sy;
bool has[size][size][size][size][size][size];
node start, ne, pre;
const int GoM[8][2] = {{1, 2}, {1, -2}, {-1, 2}, {-1, -2}, {2, 1}, {2, -1}, {-2, 1}, {-2, -1}};
const int GoM1[8][2] = {{0, 1}, {0, -1}, {0, 1}, {0, -1}, {1, 0}, {1, 0}, {-1, 0}, {-1, 0}};
const int GoC[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
char mapp[size][size];
int ans;
bool check(int x, int y)
{
return (x >= 0 && x < n && y >= 0 && y < m);
}
void bfs()
{
memset(has, false, sizeof(has));
start.step = 0;
queue <node> Que;
Que.push(start);
has[start.mx][start.my][start.px][start.py][start.cx][start.cy] = true;
while (!Que.empty()){
node pre = Que.front();
Que.pop();
for (int i = 0; i < 8; i ++){
int xx = pre.mx + GoM[i][0];
int yy = pre.my + GoM[i][1];
int judx = pre.mx + GoM1[i][0];
int judy = pre.my + GoM1[i][1];
if (!check(xx, yy) )continue;
if ((judx == pre.px && judy == pre.py) || (judx == pre.cx && judy == pre.cy)||mapp[judx][judy] == 'D' || mapp[judx][judy] == 'S')continue;
if ((xx == pre.px && yy == pre.py) || (xx == pre.cx && yy == pre.cy) || mapp[xx][yy] == 'D')continue;
if (!has[xx][yy][pre.px][pre.py][pre.cx][pre.cy]){
if (mapp[xx][yy] == 'S'){
ans = pre.step+1;
return ;
}
has[xx][yy][pre.px][pre.py][pre.cx][pre.cy] = true;
ne = pre;
ne.mx = xx, ne.my = yy;
ne.step ++;
Que.push(ne);
}
}
for (int i = 0; i < 4; i ++){
for (int k = 1; k <= 10; k ++){
int xx = pre.cx + k*GoC[i][0];
int yy = pre.cy + k*GoC[i][1];
if (!check(xx, yy) || (pre.mx == xx && pre.my == yy) || (pre.px == xx && pre.py == yy) || mapp[xx][yy] == 'D')break;
if (!has[pre.mx][pre.my][pre.px][pre.py][xx][yy]){
if (mapp[xx][yy] == 'S'){
ans = pre.step+1;
return ;
}
has[pre.mx][pre.my][pre.px][pre.py][xx][yy] = true;
ne = pre;
ne.cx = xx, ne.cy = yy;
ne.step ++;
Que.push(ne);
}
}
}
for (int i = 0; i < 4; i ++){//p
int flag = 0;
for (int k = 1; k <= 10; k ++){
int xx = pre.px + k*GoC[i][0];
int yy = pre.py + k*GoC[i][1];
if (!check(xx, yy))break;
if ((pre.mx == xx && pre.my == yy) || (pre.cx == xx && pre.cy == yy) || mapp[xx][yy] == 'D' || mapp[xx][yy] == 'S')flag ++;
if (flag == 1 && mapp[xx][yy] == 'S')break;
if (flag == 1 )continue;
if (flag > 2 )break;
if (flag == 2 && mapp[xx][yy] != 'S')break;
if (!has[pre.mx][pre.my][xx][yy][pre.cx][pre.cy]){
if (mapp[xx][yy] == 'S'){
ans = pre.step+1;
return ;
}
has[pre.mx][pre.my][xx][yy][pre.cx][pre.cy] = true;
ne = pre;
ne.px = xx, ne.py = yy;
ne.step ++;
Que.push(ne);
}
}
}
}
}
int main()
{
int nc = 0;
while (scanf("%d%d", &n, &m) != EOF){
for (int i = 0; i < n; i ++){
scanf("%s", mapp[i]);
for (int j = 0; j < m; j ++){
if (mapp[i][j] == 'M'){
start.mx = i, start.my = j;
mapp[i][j] = '.';
}
if (mapp[i][j] == 'C'){
start.cx = i, start.cy = j;
mapp[i][j] = '.';
}
if (mapp[i][j] == 'P'){
start.px = i, start.py = j;
mapp[i][j] = '.';
}
}
}
ans = -1;
printf("Scenario #%d\n", ++nc);
bfs();
if (ans == -1)printf("OH!That's impossible!\n\n");
else
printf("%d\n\n", ans);
}
return 0 ;
}

33
HDOJ/3006_autoAC.cpp Normal file
View File

@ -0,0 +1,33 @@
#include<stdio.h>
#include<string.h>
int s[1<<15];
int n,m,k,element;
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
int ans=0;
memset(s,0,sizeof(s));
while(n--)
{
int set=0;
scanf("%d",&k);
while(k--)
{
scanf("%d",&element);
set=set|(1<<(element-1));
}
s[set]=1;
for(int j=0;j<=(1<<14);j++)
{
if(s[j]){s[set|j]=1;}
}
}
for(int i=0;i<=1<<14;i++)
{
if(s[i]){ans++;}
}
printf("%d\n",ans);
}
return 0;
}

102
HDOJ/3007_autoAC.cpp Normal file
View File

@ -0,0 +1,102 @@
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <math.h>
using namespace std;
struct Point
{
double x,y;
};
Point a[1005],d;
double r;
double dist(Point A,Point B)
{
return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));
}
double cross(Point A,Point B,Point C)
{
return (B.x-A.x)*(C.y-A.y)-(C.x-A.x)*(B.y-A.y);
}
void MiniDiscWith2Point(Point p,Point q,int n)
{
d.x=(p.x+q.x)/2.0;
d.y=(p.y+q.y)/2.0;
r=dist(p,q)/2;
int k;
double c1,c2,t1,t2,t3;
for(k=1;k<=n;k++)
{
if(dist(d,a[k])<=r) continue;
if(cross(p,q,a[k])!=0.0)
{
c1=(p.x*p.x+p.y*p.y-q.x*q.x-q.y*q.y)/2.0;
c2=(p.x*p.x+p.y*p.y-a[k].x*a[k].x-a[k].y*a[k].y)/2.0;
d.x=(c1*(p.y-a[k].y)-c2*(p.y-q.y))/((p.x-q.x)*(p.y-a[k].y)-(p.x-a[k].x)*(p.y-q.y));
d.y=(c1*(p.x-a[k].x)-c2*(p.x-q.x))/((p.y-q.y)*(p.x-a[k].x)-(p.y-a[k].y)*(p.x-q.x));
r=dist(d,a[k]);
}
else
{
t1=dist(p,q);
t2=dist(q,a[k]);
t3=dist(p,a[k]);
if(t1>=t2&&t1>=t3)
{
d.x=(p.x+q.x)/2.0;
d.y=(p.y+q.y)/2.0;
r=dist(p,q)/2.0;
}
else if(t2>=t1&&t2>=t3)
{
d.x=(a[k].x+q.x)/2.0;
d.y=(a[k].y+q.y)/2.0;
r=dist(a[k],q)/2.0;
}
else
{
d.x=(a[k].x+p.x)/2.0;
d.y=(a[k].y+p.y)/2.0;
r=dist(a[k],p)/2.0;
}
}
}
}
void MiniDiscWithPoint(Point pi,int n)
{
d.x=(pi.x+a[1].x)/2.0;
d.y=(pi.y+a[1].y)/2.0;
r=dist(pi,a[1])/2.0;
int j;
for(j=2;j<=n;j++)
{
if(dist(d,a[j])<=r) continue;
else
{
MiniDiscWith2Point(pi,a[j],j-1);
}
}
}
int main()
{
int i,n;
while(scanf("%d",&n)&&n)
{
for(i=1;i<=n;i++)
scanf("%lf %lf",&a[i].x,&a[i].y);
if(n==1)
{
printf("%.2lf %.2lf 0.00\n",a[1].x,a[1].y);
continue;
}
r=dist(a[1],a[2])/2.0;
d.x=(a[1].x+a[2].x)/2.0;
d.y=(a[1].y+a[2].y)/2.0;
for(i=3;i<=n;i++)
{
if(dist(d,a[i])<=r)continue;
else MiniDiscWithPoint(a[i],i-1);
}
printf("%.2lf %.2lf %.2lf\n",d.x,d.y,r);
}
return 0;
}

58
HDOJ/3008_autoAC.cpp Normal file
View File

@ -0,0 +1,58 @@
#include<iostream>
#include<algorithm>
using namespace std;
#define inf 110
int dp[inf][inf];
#define Max(a,b) a>b?a:b
struct node
{
int ai,bi;
}b[inf];
int cmp(struct node a,struct node b){return a.ai<b.ai;}
int main()
{
int n,p,t,i,j;
while(cin>>n>>t>>p)
{
if(n==0&&t==0&&p==0)
break;
memset(b,0,sizeof(b));
for(i=1;i<=n;i++)
scanf("%d%d",&b[i].ai,&b[i].bi);
sort(b+1,b+n+1,cmp);
b[0].ai=0;
b[0].bi=1;
b[n+1].bi=b[n+1].ai=100;
memset(dp,0,sizeof(dp));
int T=(100+p-1)/p;
int k;
for(i=1;i<=T;i++)
{
for(j=0;j<=100;j++)
{
int max=0;
for(k=0;k<=n;k++)
{
if(b[k].ai+j>100)
break;
max=Max(max,dp[i-1][j+b[k].ai]+b[k].bi);
}
dp[i][j]=max;
}
for(j=99;j>100-t;j--)
dp[i][100]=Max(dp[i][j],dp[i][100]);
for(j=100-t;j>=0;j--)
dp[i][j+t]=Max(dp[i][j],dp[i][j+t]);
}
for(i=1;i<=T;i++)
{
for(j=0;j<=100;j++)
if(dp[i][j]>=100)
break;
if(j<=100)
{printf("%d\n",i);break;}
}
if(i>T)printf("My god\n");
}
return 0;
}

29
HDOJ/3011_autoAC.cpp Normal file
View File

@ -0,0 +1,29 @@
#include<stdio.h>
#include<math.h>
int main()
{
double r,a,b,c,t;
double x1,x2;
while(scanf("%lf%lf",&r,&a)!=EOF)
{
t=-1.0;
b=r-a;
c=r*r*r/a/2-2*r*r+a*r;
if(b*b-4*c<0)
{
printf("Oh, my god!\n");
continue;
}
x1=(-b+sqrt(b*b-4*c))/2;
x2=(-b-sqrt(b*b-4*c))/2;
if(x1>=0&&x1<=r)
t=x1;
else if(x2>=0&&x2<=r)
t=x2;
if(t>=0&&t<=r)
printf("%.2lf\n",t);
else
printf("Oh, my god!\n");
}
return 0;
}

89
HDOJ/3013_autoAC.cpp Normal file
View File

@ -0,0 +1,89 @@
#include <stdio.h>
#include <cstring>
#include <iostream>
using namespace std;
int dir[7][4][4][2]={
{{{0, 0}, {0, 1}, {0, 2}, {0, 3}}, {{0, 0}, {1, 0}, {2, 0}, {3, 0}}, {{0, 0}, {0, 1}, {0, 2}, {0, 3}}, {{0, 0}, {1, 0}, {2, 0}, {3, 0}}},
{{{0, 0}, {0, 1}, {0, 2}, {1, 0}}, {{0, 0}, {1, 0}, {2, 0}, {2, 1}}, {{0, 0}, {0, 1}, {0, 2}, {-1, 2}}, {{0, 0}, {0, 1}, {1, 1}, {2, 1}}},
{{{0, 0}, {0, 1}, {0, 2}, {1, 2}}, {{0, 0}, {0, 1}, {1, 0}, {2, 0}}, {{0, 0}, {1, 0}, {1, 1}, {1, 2}}, {{0, 0}, {0, 1}, {-1, 1}, {-2, 1}}},
{{{0, 0}, {0, 1}, {1, 0}, {1, 1}}, {{0, 0}, {0, 1}, {1, 0}, {1, 1}}, {{0, 0}, {0, 1}, {1, 0}, {1, 1}}, {{0, 0}, {0, 1}, {1, 0}, {1, 1}}},
{{{0, 0}, {0, 1}, {1, 1}, {1, 2}}, {{0, 0}, {1, 0}, {0, 1}, {-1, 1}}, {{0, 0}, {0, 1}, {1, 1}, {1, 2}}, {{0, 0}, {1, 0}, {0, 1}, {-1, 1}}},
{{{0, 0}, {0, 1}, {0, 2}, {1, 1}}, {{0, 0}, {1, 0}, {2, 0}, {1, 1}}, {{0, 0}, {0, 1}, {0, 2}, {-1, 1}}, {{0, 0}, {0, 1}, {-1, 1}, {1, 1}}},
{{{0, 0}, {0, 1}, {-1, 1}, {-1, 2}}, {{0, 0}, {1, 0}, {1, 1}, {2, 1}}, {{0, 0}, {0, 1}, {-1, 1}, {-1, 2}}, {{0, 0}, {1, 0}, {1, 1}, {2, 1}}}
};
int map[30][15];
int state[30][15];
int sum[30];
int safe(int x, int y)
{
if(x>0&&y>0&&y<=10&&map[x][y]==0) return 1;
return 0;
}
int main()
{
int m, id, de, pos, i, j, k, x1, y1, x2, y2, x3, y3, x4, y4, jj;
int frx1, frx2, frx3, frx4, fry1, fry2, fry3, fry4;
while(scanf("%d", &m)!=EOF)
{
memset(map, 0, sizeof(map));
memset(sum, 0, sizeof(sum));
memset(state, 0, sizeof(state));
int flag=0;
for(i=0; i<m; i++)
{
scanf("%d%d%d", &id, &de, &pos);
if(flag==1) continue;
id--; de/=90;
for(j=23; j>0; j--)
{
x1=j+dir[id][de][0][0], y1=pos+dir[id][de][0][1];
x2=j+dir[id][de][1][0], y2=pos+dir[id][de][1][1];
x3=j+dir[id][de][2][0], y3=pos+dir[id][de][2][1];
x4=j+dir[id][de][3][0], y4=pos+dir[id][de][3][1];
if(safe(x1, y1)&&safe(x2, y2)&&safe(x3, y3)&&safe(x4, y4))
{
frx1=x1, frx2=x2, frx3=x3, frx4=x4;
fry1=y1, fry2=y2, fry3=y3, fry4=y4;
}
else break;
}
if(j==23) flag=1;
if(flag) continue;
map[frx1][fry1]=1, map[frx2][fry2]=1, map[frx3][fry3]=1, map[frx4][fry4]=1;
sum[frx1]++, sum[frx2]++, sum[frx3]++, sum[frx4]++;
for(j=1; j<=20; j++)
{
while(sum[j]==10)
{
for(jj=j+1; jj<25; jj++)
{
for(k=1; k<=10; k++)
map[jj-1][k]=map[jj][k];
sum[jj-1]=sum[jj];
}
}
}
for(j=21; j<25; j++)
if(sum[j]) flag=1;
if(flag!=1)
{
for(j=0; j<=20; j++)
for(jj=0; jj<=10; jj++)
state[j][jj]=map[j][jj];
}
}
printf("+--------------------+\n");
for(i=20; i>=1; i--)
{
printf("|");
for(j=1; j<=10; j++)
{
if(state[i][j]==1) printf("[]");
else printf("..");
}
printf("|\n");
}
printf("+--------------------+\n\n");
}
return 0;
}

119
HDOJ/3015_autoAC.cpp Normal file
View File

@ -0,0 +1,119 @@
#include<iostream>
#include<algorithm>
using namespace std;
#define M 100005
struct node
{
__int64 a,b;
int num1,num2;
}t[M];
int cmpa(node &p,node &q)
{
if(p.a<q.a)
return 1;
else
return 0;
}
int cmpb(node &p,node &q)
{
if(p.b<q.b)
return 1;
else
return 0;
}
int cmpn(node &p,node &q)
{
if(p.num2<q.num2)
return 1;
else
return 0;
}
__int64 c[M],d[M],n;
int lowbit(int x)
{
return x&(-x);
}
void updatac(int i,int j)
{
while(i<=n)
{
c[i]+=j;
i+=lowbit(i);
}
}
void updatad(int i,int j)
{
while(i<=n)
{
d[i]+=j;
i+=lowbit(i);
}
}
__int64 getsumc(int x)
{
__int64 s=0;
while(x>0)
{
s+=c[x];
x-=lowbit(x);
}
return s;
}
__int64 getsumd(int x)
{
__int64 s=0;
while(x>0)
{
s+=d[x];
x-=lowbit(x);
}
return s;
}
int main()
{
while(scanf("%d",&n)>0)
{
memset(c,0,sizeof(c));
memset(d,0,sizeof(d));
int i;
for(i=1;i<=n;i++)
{
scanf("%I64d%I64d",&t[i].a,&t[i].b);
}
sort(t+1,t+1+n,cmpa);
for(i=1;i<=n;i++)
{
if(i!=1&&t[i].a==t[i-1].a)
t[i].num1=t[i-1].num1;
else
t[i].num1=i;
}
int max=t[n].num1;
sort(t+1,t+1+n,cmpb);
for(i=1;i<=n;i++)
{
if(i!=1&&t[i].b==t[i-1].b)
t[i].num2=t[i-1].num2;
else
t[i].num2=i;
}
sort(t+1,t+1+n,cmpn);
for(i=1;i<=n;i++)
{
updatac(t[i].num1,t[i].num1);
updatad(t[i].num1,1);
}
__int64 sum=0,m=n,tmp1,tmp2;
for(i=1;i<n;i++)
{
tmp1=getsumd(t[i].num1-1)*t[i].num1-getsumc(t[i].num1-1);
tmp2=(getsumd(max)-getsumd(t[i].num1))*t[i].num1-(getsumc(max)-getsumc(t[i].num1));
sum+=(tmp1-tmp2)*t[i].num2;
updatac(t[i].num1,-t[i].num1);
updatad(t[i].num1,-1);
m--;
}
printf("%I64d\n",sum);
}
return 0;
}

108
HDOJ/3016_autoAC.cpp Normal file
View File

@ -0,0 +1,108 @@
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 100010
#define lch(i) ((i)<<1)
#define rch(i) ((i)<<1|1)
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
int n;
int dp[N];
struct seg
{
int h,l,r,v;
int ln,rn;
}s[N];
struct node
{
int mark,l,r,n;
int mid(){
return (l+r)>>1;
}
}t[4*N];
int cmp_h(struct seg x ,struct seg y)
{
return x.h < y.h;
}
void build(int l , int r , int rt)
{
t[rt].l = l; t[rt].r = r;
t[rt].mark = 0;
t[rt].n = -1;
if(l == r) return ;
int mid = t[rt].mid();
int ll = lch(rt);
int rr = rch(rt);
build(l , mid , ll);
build(mid+1 , r , rr);
}
int query(int x ,int rt)
{
if(t[rt].l == t[rt].r)
return t[rt].n;
int mid = t[rt].mid();
int ll = lch(rt);
int rr = rch(rt);
if(t[rt].mark != -1)
{
t[ll].mark = t[rr].mark = t[rt].mark;
t[ll].n = t[rr].n = t[rt].n;
return t[rt].n;
}
if(x<=mid) return query(x , ll);
else return query(x , rr);
}
void updata(int l , int r ,int m , int rt)
{
if(t[rt].l == l && t[rt].r == r)
{
t[rt].mark = 1;
t[rt].n = m;
return ;
}
int mid = t[rt].mid();
int ll = lch(rt);
int rr = rch(rt);
if(t[rt].mark != -1)
{
t[ll].mark = t[rr].mark = t[rt].mark;
t[ll].n = t[rr].n = t[rt].n;
t[rt].mark = -1;
}
if(r<=mid) updata(l,r,m,ll);
else if(l>mid) updata(l,r,m,rr);
else
{
updata(l,mid,m,ll);
updata(mid+1,r,m,rr);
}
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
n++; s[0].h = 0; s[0].l = 1; s[0].r = 100000; s[0].v = 0;
for(int i=1; i<n; i++)
scanf("%d%d%d%d",&s[i].h, &s[i].l, &s[i].r, &s[i].v);
sort(s,s+n,cmp_h);
build(1,100000,1);
for(int i=0; i<n; i++)
{
s[i].ln = query(s[i].l , 1);
s[i].rn = query(s[i].r , 1);
updata(s[i].l , s[i].r , i , 1);
}
memset(dp,0,sizeof(dp));
dp[n-1] = 100 + s[n-1].v;
for(int i=n-1; i>=0; i--)
{
int ln = s[i].ln;
int rn = s[i].rn;
dp[ln] = max(dp[ln] , dp[i] + s[ln].v);
dp[rn] = max(dp[rn] , dp[i] + s[rn].v);
}
printf("%d\n",dp[0] = (dp[0]>0?dp[0]:-1) );
}
return 0;
}

61
HDOJ/3018_autoAC.cpp Normal file
View File

@ -0,0 +1,61 @@
#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
using namespace std;
const int maxn = 100005;
int n, m;
int fa[maxn];
int deg[maxn];
int used[maxn];
int odd[maxn];
vector<int> v;
void init()
{
v.clear();
for (int i=1; i<=n; i++) fa[i] = i, deg[i] = 0, used[i] = 0, odd[i] = 0;
}
int find(int x)
{
while (x != fa[x]) x = fa[x];
return x;
}
int main()
{
while (scanf("%d %d", &n, &m) != EOF)
{
init();
int i;
for (i=0; i<m; i++)
{
int a, b;
scanf("%d %d", &a, &b);
int faa = find(a);
int fab = find(b);
deg[a] ++;
deg[b] ++;
fa[fab] = faa;
}
for (i=1; i<=n; i++)
{
int f = find(i);
if (!used[f])
{
v.push_back(f);
used[f] = 1;
}
if (deg[i] & 1)
odd[f] ++;
}
int sum = 0;
for (i=0; i<v.size(); i++)
{
int k = v[i];
if (deg[k] == 0) continue;
if (odd[k] == 0) sum ++;
else sum += odd[k] / 2;
}
printf("%d\n", sum);
}
return 0;
}

129
HDOJ/3023_autoAC.cpp Normal file
View File

@ -0,0 +1,129 @@
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#define N 1001
using namespace std;
int fx[8][2] = { 0, 1, 0, -1, 1, 0, -1, 0, 1, 1, 1, -1, -1, 1, -1, -1 }, n, m, sx, sy, ex, ey, tp, tb;
struct Map
{
int cb;
int path;
int x;
int y;
char c;
friend bool operator<(Map t1, Map t2)
{
if (t1.cb == t2.cb)
{
return t1.path>t2.path;
}
return t1.cb>t2.cb;
}
};
Map map[N][N];
void input()
{
int i, j, k;
scanf("%d%d%d%d", &sx, &sy, &ex, &ey);
for (i = 1; i <= n; i++)
{
getchar();
for (j = 1; j <= m; j++)
{
map[i][j].c = getchar();
map[i][j].path = -1;
}
}
}
bool isvalide(int x, int y)
{
if (x<1 || x>n || y<1 || y>m || map[x][y].c == '0')
return false;
return true;
}
void BFS()
{
int i, j, k;
priority_queue<Map> qu;
Map t1, t2;
int tx, ty;
map[sx][sy].path = 0;
map[sx][sy].cb = 0;
t1 = map[sx][sy];
t1.x = sx;
t1.y = sy;
qu.push(t1);
while (!qu.empty())
{
t2 = qu.top();
if (t2.cb>tb || (t2.cb == tb&&t2.path >= tp))
return;
qu.pop();
for (i = 0; i<8; i++)
{
tx = t2.x + fx[i][0];
ty = t2.y + fx[i][1];
if (isvalide(tx, ty))
{
if (map[tx][ty].path != -1)
{
if (map[tx][ty].c != t2.c)
{
if (map[tx][ty].cb<t2.cb + 1 || (map[tx][ty].cb == t2.cb + 1 && map[tx][ty].path <= t2.path + 1))
continue;
map[tx][ty].cb = t2.cb + 1;
map[tx][ty].path = t2.path + 1;
}
else
{
if (map[tx][ty].cb<t2.cb || (map[tx][ty].cb == t2.cb&&map[tx][ty].path <= t2.path + 1))
continue;
map[tx][ty].cb = t2.cb;
map[tx][ty].path = t2.path + 1;
}
}
else
{
if (map[tx][ty].c != t2.c)
{
map[tx][ty].cb = t2.cb + 1;
map[tx][ty].path = t2.path + 1;
}
else
{
map[tx][ty].cb = t2.cb;
map[tx][ty].path = t2.path + 1;
}
}
t1 = map[tx][ty];
t1.x = tx;
t1.y = ty;
if (tx == ex&&ey == ty)
{
if (t1.cb<tb)
{
tb = t1.cb;
tp = t1.path;
}
else if (t1.cb == tb&&t1.path<tp)
{
tp = t1.path;
}
continue;
}
qu.push(t1);
}
}
}
}
int main()
{
while (scanf("%d%d", &n, &m) != EOF)
{
input();
tb = tp = 100000;
BFS();
printf("%d %d\n", tp + 1, tb);
}
}

33
HDOJ/3024_autoAC.cpp Normal file
View File

@ -0,0 +1,33 @@
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <string>
#include <string>
#include <cmath>
using namespace std;
#define maxn 480
int dp[maxn][maxn][2];
int pos[maxn];
inline int min(int a,int b)
{
return a>b?b:a;
}
int main()
{
int t; scanf("%d",&t);
while(t--){
int n; scanf("%d",&n);
for(int i = 1;i <= n;i++) scanf("%d",&pos[i]);
sort(pos+1,pos+1+n);
for(int i = 1;i <= n;i++) dp[i][i][0] = dp[i][i][1] = abs(pos[i])*n;
for(int i = n;i >= 1;i--)
for(int j = i+1;j <= n;j++){
dp[i][j][0] = min(dp[i + 1][j][0] + abs(pos[i + 1] - pos[i]) * (n - j + i), dp[i + 1][j][1] + abs(pos[j] - pos[i]) * (n - j + i));
dp[i][j][1] = min(dp[i][j - 1][0] + abs(pos[i] - pos[j]) * (n - j + i), dp[i][j - 1][1] + abs(pos[j - 1] - pos[j]) * (n - j + i));
}
printf("%d\n",min(dp[1][n][0],dp[1][n][1]));
}
return 0;
}

86
HDOJ/3028_autoAC.cpp Normal file
View File

@ -0,0 +1,86 @@
#include<cstdio>
#include<set>
#include<cstring>
using namespace std;
#define max(a,b) ((a)>(b)?(a):(b))
int dp[1005][105][2];
int times[105][1005];
int alltime[105][1005];
struct node
{
int x,t;
bool operator < (const node &a) const
{
return x<a.x;
}
} temp;
set<node> line[105];
int check(int x,int t)
{
int sumt=0;
for(set<node>::iterator it=line[x].begin(); it!=line[x].end(); ++it)
sumt+=times[it->x][t];
return sumt+times[x][t];
}
int main()
{
int n,m,t,p,q,d,x,a,k;
for(; ~scanf("%d%d%d",&n,&m,&t);)
{
int maxx[2];
maxx[0]=maxx[1]=0;
memset(dp,-1,sizeof(dp));
memset(times,0,sizeof(times));
for(int i=0; i<=n; ++i) line[i].clear();
for(int i=0; i<m; ++i)
{
scanf("%d%d%d",&p,&q,&d);
temp.t=d;
temp.x=q;
line[p].insert(temp);
temp.x=p;
line[q].insert(temp);
}
for(;scanf("%d%d%d",&x,&a,&k);)
{
if(x+a+k==0) break;
times[a][x]+=k;
}
for(int i=1; i<=n; ++i)
for(int j=1; j<=t; ++j)
alltime[i][j]=check(i,j);
for(int i=1; i<=n; ++i)
{
dp[1][i][0]=times[i][1];
dp[1][i][1]=alltime[i][1];
}
for(int i=1; i<=t; ++i)
for(int j=1; j<=n; ++j)
{
if(dp[i][j][0]!=-1)
{
for(set<node>::iterator it=line[j].begin(); it!=line[j].end(); ++it)
{
if(i+it->t<=t)
{
dp[i+it->t][it->x][0]=max(dp[i+it->t][it->x][0],dp[i][j][0]+times[it->x][i+it->t]);
dp[i+it->t][it->x][1]=max(dp[i+it->t][it->x][1],dp[i][j][0]+alltime[it->x][i+it->t]);
dp[i+it->t][it->x][1]=max(dp[i+it->t][it->x][1],dp[i][j][1]+times[it->x][i+it->t]);
maxx[0]=max(maxx[0],dp[i+it->t][it->x][0]);
maxx[1]=max(maxx[1],dp[i+it->t][it->x][1]);
}
}
if(i+1<=t)
{
dp[i+1][j][0]=max(dp[i+1][j][0],dp[i][j][0]+times[j][i+1]);
dp[i+1][j][1]=max(dp[i+1][j][1],dp[i][j][0]+alltime[j][i+1]);
dp[i+1][j][1]=max(dp[i+1][j][1],dp[i][j][1]+times[j][i+1]);
maxx[0]=max(maxx[0],dp[i+1][j][0]);
maxx[1]=max(maxx[1],dp[i+1][j][1]);
}
}
}
printf("%d %d\n",maxx[1],maxx[0]);
}
return 0;
}

63
HDOJ/3029_autoAC.cpp Normal file
View File

@ -0,0 +1,63 @@
#include <iostream>
#include <cmath>
using namespace std;
int num[30];
int ind;
void solve(int n)
{
ind = 0;
while(n)
{
num[ind++] = n % 3;
n /= 3;
}
}
void ss(int ii)
{
num[ii]+=1;
for(int i=ii;i<ind;i++)
{
if(num[i]>2)
{
num[i] = 0;
num[i+1]++;
if(i == ind-1) ind += 1;
}
}
}
int main()
{
int n;
while( cin >> n)
{
memset(num , 0 ,sizeof(num));
solve(n);
int re[30];
int sum = 0,countd=0;
memset(re,0,sizeof(re));
for(int i=0;i<ind;i++)
{
if(num[i]==2)
{
sum++;
ss(i);
re[i] = 1;
}
}
for(int i=0;i<ind;i++)
if(num[i] == 1) countd++;
if(sum == 0) cout << "0" << endl;
else
{
cout <<sum;
for(int i=0;i<ind;i++)
if(re[i]) printf(" %.lf",pow(3.0,i));
cout << endl;
}
cout << countd;
for(int i=0;i<ind;i++)
if(num[i] == 1) printf(" %.lf",pow(3.0,i));
cout << endl;
}
return 0;
}

75
HDOJ/3030_autoAC.cpp Normal file
View File

@ -0,0 +1,75 @@
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define lowbit(x) x&(-x)
using namespace std;
const int maxn = 500050;
const __int64 mod =1000000007;
__int64 a[maxn],b[maxn],c[maxn],e[maxn];
int k;
int find(__int64 key)
{
int l=1,r=k;
while(l<=r)
{
int mid = (l+r)>>1;
if(b[mid]==key) return mid;
if(b[mid]<key)
l=mid+1;
else
r=mid-1;
}
}
void insert(int p,int d)
{
for(int i=p;i<=k;i+=lowbit(i))
{
e[i]+=d;
e[i]%=mod;
}
}
__int64 query(int p)
{
__int64 sum=0;
for(int i=p;i>0;i-=lowbit(i))
{
sum+=e[i];
sum%=mod;
}
return sum;
}
int main(int argc, char *argv[])
{
int t,cas=1;
__int64 n,m,x,y,z;
scanf("%d",&t);
while(t--)
{
scanf("%I64d%I64d%I64d%I64d%I64d",&n,&m,&x,&y,&z);
for(int i=0;i<m;i++)
scanf("%I64d",&a[i]);
for(int i=0;i<=n-1;i++)
{
b[i+1]=a[i%m];
c[i+1]=b[i+1];
a[i%m]=(x*a[i%m]+y*(i+1))%z;
}
sort(b+1,b+1+n);
k = 1;
for(int i=2;i<=n;i++)
if(b[i]!=b[i-1]) b[++k]=b[i];
memset(e,0,sizeof(e));
__int64 sum= 0 ;
for(int i=1;i<=n;i++)
{
int ans = find(c[i]);
int num=query(ans-1);
num%=mod;
sum+=num+1;
sum%=mod;
insert(ans,num+1);
}
printf("Case #%d: %I64d\n",cas++,sum);
}
return 0;
}

96
HDOJ/3031_autoAC.cpp Normal file
View File

@ -0,0 +1,96 @@
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
using namespace std;
#define MAXN 2000010
int r, n, m, cnt[10010], p[10010];
int fa[10010], rt[2], s[2];
int tot, x, sh, sw, h, w;
char op[4];
struct Node
{
int v, l, r, dis;
Node() {}
Node(int _v, int _l, int _r, int _d):
v(_v), l(_l), r(_r), dis(_d) {}
}nn[MAXN];
int merge(int x, int y)
{
if(!x) return y;
if(!y) return x;
if(nn[x].v < nn[y].v) swap(x, y);
nn[x].r = merge(nn[x].r, y);
if(nn[nn[x].l].dis < nn[nn[x].r].dis) swap(nn[x].l, nn[x].r);
nn[x].dis = nn[nn[x].r].dis + 1;
return x;
}
int main()
{
// freopen("G.in", "r", stdin);
while(~scanf("%d", &r))
{
h = w = 0;
while(r--)
{
tot = 1;
s[0] = s[1] = rt[0] = rt[1] = 0;
memset(fa, 0, sizeof(fa));
scanf("%d%d", &n, &m);
for(int i = 1; i <= m; i++) scanf("%d", cnt + i);
for(int i = 1; i <= m; i++) for(int j = 1; j <= cnt[i]; j++)
{
scanf("%d", &x);
nn[tot] = Node(x, 0, 0, 0);
fa[i] = merge(fa[i], tot);
tot++;
}
for(int i = 1; i <= n; i++)
{
scanf("%s", op);
if(op[0] == 'T')
{
scanf("%d", &x);
rt[1 & i] = merge(rt[1 & i], fa[x]);
s[1 & i] += cnt[x];
}
if(op[0] == 'C')
{
if(nn[rt[0]].v > nn[rt[1]].v)
{
rt[0] = merge(rt[0], rt[1]);
s[0] += s[1]; rt[1] = s[1] = 0;
}
if(nn[rt[0]].v < nn[rt[1]].v)
{
rt[1] = merge(rt[0], rt[1]);
s[1] += s[0]; rt[0] = s[0] = 0;
}
}
if(op[0] == 'L')
{
rt[1 & i] = merge(nn[rt[1 & i]].l, nn[rt[1 & i]].r);
s[1 & i]--;
}
if(op[0] == 'A')
{
scanf("%d", &x);
nn[rt[1 & i]].v += x;
}
if(op[0] == 'E')
{
scanf("%d", &x);
int lson = nn[rt[1 & i]].l;
int rson = nn[rt[1 & i]].r;
nn[rt[1 & i]] = Node(x, 0, 0, 0);
rt[1 & i] = merge(merge(lson, rson), rt[1 & i]);
}
}
if(s[0] > s[1]) h++; else w++;
printf("%d:%d\n", s[1], s[0]);
}
if(h > w) puts("I will be back!!"); else puts("Hahaha...I win!!");
}
return 0;
}

25
HDOJ/3032_autoAC.cpp Normal file
View File

@ -0,0 +1,25 @@
#include<stdio.h>
int main ()
{
int i,s,n,a,t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(s=0,i=1;i<=n;i++)
{
scanf("%d",&a);
if(a%4==0)
s^=(a-1);
else if(a%4==3)
s^=(a+1);
else
s^=a;
}
if(s)
printf("Alice\n");
else
printf("Bob\n");
}
return 0;
}

45
HDOJ/3033_autoAC.cpp Normal file
View File

@ -0,0 +1,45 @@
#include<stdio.h>
#include<string.h>
int dp[15][10010];
int max(int a,int b)
{
if(a>b)
return a;
else
return b;
}
int main()
{
int i,j,n,m;
int k,T,num[15],v[15][151],c[15][150];
while(scanf("%d%d%d",&n,&m,&T)!=EOF)
{
memset(v,0,sizeof(v));
memset(c,0,sizeof(c));
memset(num,0,sizeof(num));
memset(dp,-1,sizeof(dp));
memset(dp[0],0,sizeof(dp[0]));
for(i=1;i<=n;i++)
{
scanf("%d",&j);
num[j]++;
scanf("%d%d",&c[j][num[j]],&v[j][num[j]]);
}
for(i=1;i<=T;i++)
{
for(k=1;k<=num[i];k++)
for(j=m;j>=c[i][k];j--)
{
if(dp[i][j-c[i][k]]!=-1)
dp[i][j]=max(dp[i][j],dp[i][j-c[i][k]]+v[i][k]);
if(dp[i-1][j-c[i][k]]!=-1)
dp[i][j]=max(dp[i][j],dp[i-1][j-c[i][k]]+v[i][k]);
}
}
if(dp[T][m]<0)
printf("Impossible\n");
else
printf("%d\n",dp[T][m]);
}
return 0;
}

109
HDOJ/3034_autoAC.cpp Normal file
View File

@ -0,0 +1,109 @@
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define MaxN 40
#define MaxS 2000010
using namespace std;
int n, m, N, px, st, ed, hs, dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
int d[MaxS], dist[MaxN], len[MaxN], g[MaxN][4], pN[5];
bool adj[MaxN][MaxN];
char s[MaxN][MaxN];
struct HeapNode {
int s, f;
HeapNode(){}
HeapNode(int s, int f):s(s),f(f){}
}heap[MaxS];
void sink(int p)
{
HeapNode a = heap[p];
for (int q = p<<1; q <= hs; p = q, q <<= 1) {
if (q < hs && heap[q+1].f < heap[q].f) ++q;
if (heap[q].f >= a.f) break;
heap[p] = heap[q];
}
heap[p] = a;
}
HeapNode del()
{
HeapNode r = heap[1];
heap[1] = heap[hs--];
sink(1);
return r;
}
void swim(int p)
{
HeapNode a = heap[p];
for (int q = p>>1; q && a.f < heap[q].f; p = q, q >>= 1)
heap[p] = heap[q];
heap[p] = a;
}
void ins(HeapNode a)
{
heap[++hs] = a;
swim(hs);
}
int h(int s)
{
int ret = 0;
for (int i = 0; i < m; ++i, s /= N) ret += dist[s%N];
return ret;
}
int AStar()
{
memset(d, 0x3f, sizeof(d));
int p[5];
hs = 0;
ins(HeapNode(st, h(st)));
d[st] = 0;
while (hs > 0) {
HeapNode u = del();
int s = u.s, first;
for (int i = 0; i < m; ++i, s/=N) p[i] = s%N;
for (int i = 0; i < m; ++i) if (p[i] != px) {first = i; break;}
for (int i = first; i < m; ++i)
for (int j = 0; j < len[p[i]]; ++j) {
int np = g[p[i]][j];
bool fail = 0;
for (int k = first; k < m; ++k) if (k != i && adj[np][p[k]]) {fail = 1;break;}
if (!fail) {
int v = u.s+pN[i]*(np-p[i]);
if (d[u.s] + 1 < d[v]) {
d[v] = d[u.s] + 1;
if (v == ed) return d[v];
ins(HeapNode(v, d[v] + h(v)));
}
}
}
}
return -1;
}
int main()
{
int T;
scanf("%d", &T);
while (T--) {
scanf("%d%d", &n, &m); N = n*n; st = ed = 0;
memset(adj, 0, sizeof(adj));
memset(len, 0, sizeof(len));
pN[0] = 1; for (int i = 1; i <= m; ++i) pN[i] = pN[i-1] * N;
for (int i = 0; i < n; ++i) scanf("%s", s[i]);
for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) {
int s1 = i*n+j;
if (s[i][j] == 'x') px = s1;
else if (s[i][j] != 'o') st += pN[s[i][j]-'1'] * s1;
for (int d = 0; d < 4; ++d) {
int x = i + dx[d], y = j + dy[d], s2 = x*n+y;
if (x >= 0 && x < n && y >= 0 && y < n) {
g[s1][len[s1]++] = s2;
adj[s1][s2] = 1;
}
}
}
for (int i = 0; i < m; ++i) ed = ed*N + px;
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
dist[i*n+j] = abs(i-px/n) + abs(j-px%n);
printf("%d\n", AStar());
}
return 0;
}

118
HDOJ/3035_autoAC.cpp Normal file
View File

@ -0,0 +1,118 @@
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
#define FOR(i,a,b) for(i = (a); i < (b); ++i)
#define FORE(i,a,b) for(i = (a); i <= (b); ++i)
#define CLR(a,b) memset(a,b,sizeof(a))
const int inf = 1 << 30;
const int MAXN = 502000;
const int MAXM = 3100000;
struct node {
int c,u,v,next;
}edge[MAXM];
int N, M;
int ne, head[MAXN];
int cur[MAXN], ps[MAXN], dep[MAXN];
void addedge(int u, int v,int w) {
edge[ne].u = u;
edge[ne].v = v;
edge[ne].c = w;
edge[ne].next = head[u];
head[u] = ne ++;
edge[ne].u = v;
edge[ne].v = u;
edge[ne].c = w;
edge[ne].next = head[v];
head[v] = ne ++;
}
int dinic(int s, int t) {
int tr, res = 0;
int i, j, k, f, r, top;
while(1) {
memset(dep, -1, sizeof(dep));
for(f = dep[ps[0] = s] = 0, r = 1; f != r;)
for(i = ps[f++], j = head[i]; j != -1; j = edge[j].next)
if(edge[j].c > 0 && dep[k = edge[j].v] == -1) {
dep[k] = dep[i] + 1;
ps[r++] = k;
if(k == t) {
f = r; break;
}
}
if(dep[t] == -1) break;
memcpy(cur, head, sizeof(cur));
i = s, top = 0;
while(1) {
if(i == t) {
for(tr = inf, k = 0; k < top; k++)
if(edge[ps[k]].c < tr)
tr = edge[ps[f = k]].c;
for(k = 0; k < top; k++) {
edge[ps[k]].c -= tr;
edge[ps[k]^1].c += tr;
}
i = edge[ps[top=f]].u;
res += tr;
}
for(j = cur[i]; cur[i] != -1; j = cur[i] = edge[cur[i]].next) {
if(edge[j].c > 0 && dep[i]+1 == dep[edge[j].v]) break;
}
if(cur[i] != -1) {
ps[top++] = cur[i];
i = edge[cur[i]].v;
}
else {
if(top == 0) break;
dep[i] = -1;
i = edge[ps[--top]].u;
}
}
}
return res;
}
int main() {
int i, j, x, y;
while(scanf("%d %d", &N, &M) == 2) {
ne = 2;
CLR(head, -1);
CLR(cur, 0);
CLR(ps, 0);
CLR(dep, 0);
FORE(i, 1, N + 1) {
FORE(j, 1, M) {
scanf("%d", &x);
addedge(j + (i - 1) * (M + 1), j + (i - 1) * (M + 1) + 1, x);
}
}
FORE(i, 1, N) {
FORE(j, 1, M + 1) {
scanf("%d", &x);
addedge(j + (i - 1) * (M + 1), j + (i - 1) * (M + 1) + (M + 1), x);
}
}
int tmp_cnt1 = (M + 1) * (N + 1) + 1, tmp_cnt2 = (M + 1) * (N + 1) + 1;
int cntt = 1;
FORE(i, 1, N) {
int tt1 = tmp_cnt1, tt2 = tmp_cnt2;
FORE(j, 1, M) {
scanf("%d %d", &x, &y);
addedge(cntt, tt1++, x);
addedge(cntt + 1, tt2++, y);
++cntt;
}
++cntt;
FORE(j, 1, M) {
scanf("%d %d", &x, &y);
addedge(cntt, tmp_cnt1++, x);
addedge(cntt + 1, tmp_cnt2++, y);
++cntt;
}
++cntt;
cntt -= (M + 1);
}
int endd = dinic(1, (N + 1) * (M + 1));
printf("%d\n", endd);
}
return 0;
}

158
HDOJ/3036_autoAC.cpp Normal file
View File

@ -0,0 +1,158 @@
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
#define ll int
#define N 20050
#define M 105000
#define inf 10737418
struct Edge{
ll from, to, cap, nex;
}edge[M*4];
ll head[N], edgenum;
void add(ll u, ll v, ll cap){
Edge E = { u, v, cap, head[u]};
edge[ edgenum ] = E;
head[u] = edgenum ++;
Edge E2= { v, u, 0, head[v]};
edge[ edgenum ] = E2;
head[v] = edgenum ++;
}
ll sign[N];
bool BFS(ll from, ll to){
memset(sign, -1, sizeof(sign));
sign[from] = 0;
queue<ll>q;
q.push(from);
while( !q.empty() ){
int u = q.front(); q.pop();
for(ll i = head[u]; i!=-1; i = edge[i].nex)
{
ll v = edge[i].to;
if(sign[v]==-1 && edge[i].cap)
{
sign[v] = sign[u] + 1, q.push(v);
if(sign[to] != -1)return true;
}
}
}
return false;
}
ll Stack[N], top, cur[N];
ll dinic(ll from, ll to){
ll ans = 0;
while( BFS(from, to) )
{
memcpy(cur, head, sizeof(head));
ll u = from; top = 0;
while(1)
{
if(u == to)
{
ll flow = inf, loc;
for(ll i = 0; i < top; i++)
if(flow > edge[ Stack[i] ].cap)
{
flow = edge[Stack[i]].cap;
loc = i;
}
for(ll i = 0; i < top; i++)
{
edge[ Stack[i] ].cap -= flow;
edge[Stack[i]^1].cap += flow;
}
ans += flow;
top = loc;
u = edge[Stack[top]].from;
}
for(ll i = cur[u]; i!=-1; cur[u] = i = edge[i].nex)
if(edge[i].cap && (sign[u] + 1 == sign[ edge[i].to ]))break;
if(cur[u] != -1)
{
Stack[top++] = cur[u];
u = edge[ cur[u] ].to;
}
else
{
if( top == 0 )break;
sign[u] = -1;
u = edge[ Stack[--top] ].from;
}
}
}
return ans;
}
void init(){memset(head,-1,sizeof head);edgenum = 0;}
char mp[15][15];
int n, m, T;
int Hash(int x,int y){return x*m+y;}
vector<int>E,P;
int dis[150][150], step[4][2]={1,0,-1,0,0,1,0,-1};
bool vis[150][150];
void bfs(int sx,int sy){
int start = Hash(sx,sy);
memset(vis, 0, sizeof vis);
vis[sx][sy] = 1;
dis[start][start] = 0;
queue<int>qx,qy; while(!qx.empty())qx.pop(); while(!qy.empty())qy.pop();
qx.push(sx), qy.push(sy);
while(!qx.empty()){
int x = qx.front(), y = qy.front();
qx.pop(); qy.pop();
for(int i = 0; i < 4; i++){
int dx = x + step[i][0], dy = y + step[i][1];
if(!(0<=dx&&dx<n&&0<=dy&&dy<m))continue;
if(vis[dx][dy] || mp[dx][dy]!='.')continue;
vis[dx][dy] = 1;
dis[Hash(dx,dy)][start] = dis[start][Hash(dx,dy)] = dis[start][Hash(x,y)]+1;
qx.push(dx); qy.push(dy);
}
}
}
bool ok(int TIME){
init();
int from = N-2, to = N-1;
for(int i = 0; i < P.size(); i++)add(from, P[i], 1);
for(int i = 0; i < P.size(); i++)
{
for(int j = 0; j < E.size(); j++)
if(dis[P[i]][E[j]]<=TIME) add(P[i],j*150+150+dis[P[i]][E[j]],1);
}
for(int i = 0; i < E.size(); i++)
for(int j = 1; j <= TIME; j++)
{
add(i*150+150+j, to, 1);
if(j!=TIME)
add(i*150+150+j,i*150+150+j+1,inf);
}
return dinic(from,to)==P.size();
}
int main(){
int i, j;
while(~scanf("%d %d %d",&n,&m,&T)){
E.clear(); P.clear();
memset(dis, 0, sizeof dis);
memset(mp, 0, sizeof mp);
for(i=0;i<n;i++)scanf("%s",mp[i]);
for(i=0;i<n;i++)for(j=0;j<m;j++)
{
if(mp[i][j]=='E')E.push_back(Hash(i,j));
else if(mp[i][j]=='.')P.push_back(Hash(i,j));
}
if(P.size()==0){puts("0");continue;} if(E.size()==0){puts("impossible");continue;}
for(i = 0; i < E.size(); i++)bfs(E[i]/m, E[i]%m);
int l = 1, r = 256, ans = inf;
while(l<=r)
{
int mid = (l+r)>>1;
if(ok(mid))ans = min(ans, mid), r = mid-1;
else l = mid+1;
}
if(T<ans || ans==inf){puts("impossible");continue;}
else printf("%d\n",ans);
}
return 0;
}

42
HDOJ/3037_autoAC.cpp Normal file
View File

@ -0,0 +1,42 @@
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
typedef long long ll;
ll fac[100003];
void init(ll p) {
fac[0] = 1;
for (int i=1; i<=p; i++) fac[i] = fac[i-1]*i%p;
}
ll PowerMod(ll a, ll b, ll k) {
ll tmp = a, ret = 1;
while (b) {
if (b & 1) ret = ret * tmp % k;
tmp = tmp * tmp % k;
b >>= 1;
}
return ret;
}
ll Lucas(ll n, ll m, ll p) {
ll ret = 1;
while (n && m) {
ll nn = n%p, mm = m%p;
if (nn < mm) return 0;
ret = ret*fac[nn]*PowerMod(fac[mm]*fac[nn-mm]%p, p-2, p)%p;
n /= p;
m /= p;
}
return ret;
}
int main() {
int T;
ll n, m, p;
cin >> T;
while (T--) {
cin >> n >> m >> p;
init(p);
cout << Lucas(n+m, m, p) << endl;
}
return 0;
}

57
HDOJ/3038_autoAC.cpp Normal file
View File

@ -0,0 +1,57 @@
#include<stdio.h>
#define MAXN 200010
int f[MAXN],r[MAXN];
int find(int x)
{
if(x==f[x])
return f[x];
int t=find(f[x]);
r[x]=r[x]+r[f[x]];
f[x]=t;
return f[x];
}
int fun(int x,int y)
{
if(x>y)
return x-y;
else y-x;
}
int Union(int x,int y,int sum)
{
int a=find(x);
int b=find(y);
if(a==b)
{
if(fun(r[x],r[y])==sum)
return 0;
else return 1;
}
else
{
f[a]=b;
r[a]=r[y]+sum-r[x];
return 0;
}
}
int main()
{
int n,m,i,ans,a,b,s;
while(scanf("%d %d",&n,&m)!=EOF)
{
ans=0;
for(i=0;i<=n;i++)
{
f[i]=i;
r[i]=0;
}
for(i=1;i<=m;i++)
{
scanf("%d %d %d",&a,&b,&s);
a--;
if(Union(a,b,s))
ans++;
}
printf("%d\n",ans);
}
return 0;
}

52
HDOJ/3039_autoAC.cpp Normal file
View File

@ -0,0 +1,52 @@
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
double a[105][205],b[105],ans;
int main()
{
int n,q,m;
while(scanf("%d%d",&n,&q)!=EOF)
{
m=2*n;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
scanf("%lf",&a[i][j]);
if(i==j)
a[i][n+j]=1;
else
a[i][n+j]=0;
}
for(int i=1;i<=n;i++)
{
int pos=i;
for(;pos<=n;pos++)
if(fabs(a[pos][i])>1e-6)break;
if(pos!=i)
for(int k=i;k<=m;k++)
swap(a[i][k],a[pos][k]);
double temp=a[i][i];
for(int j=i;j<=m;j++)a[i][j]/=temp;
for(int j=1;j<=n;j++)
{
if(j==i)continue;
temp=a[j][i];
for(int k=i;k<=m;k++)
a[j][k]-=a[i][k]*temp;
}
}
while(q--)
{
for(int i=1;i<=n;i++)
scanf("%lf",&b[i]);
for(int i=1;i<=n;i++)
{
ans=0;
for(int j=n+1;j<=m;j++)
ans+=a[i][j]*b[j-n];
printf("%.2f%c",ans,i==n?'\n':' ');
}
}
}
}

62
HDOJ/3040_autoAC.cpp Normal file
View File

@ -0,0 +1,62 @@
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 50005
struct node
{
int x;
__int64 tt;
int id;
}a[N],b[N];
int mark[15];
int fun(char *s)
{
int a,b,c;
sscanf(s,"%d:%d:%d",&a,&b,&c);
return a*3600+b*60+c;
}
int cmp(const void *a,const void *b)
{
node *c,*d;
c=(node *)a;d=(node *)b;
if(c->tt!=d->tt)
return c->tt<d->tt?1:-1;
else
return c->x-d->x;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int i,j,k;
j=0;
char s[15];
while(scanf("%s",s),strcmp(s,"#end")!=0)
{
a[j].x=fun(s);
scanf("%I64d%d",&a[j].tt,&a[j].id);
j++;
}
qsort(a,j,sizeof(a[0]),cmp);
k=1;
b[0]=a[0];
for(i=1;i<j;i++)
{
if(a[i].tt!=a[i-1].tt||a[i].x-a[i-1].x>=60)
b[k++]=a[i];
}
memset(mark,0,sizeof(mark));
for(i=0;i<k;i++)
mark[b[i].id]++;
printf("The result is :\n");
for(i=1;i<=n;i++)
{
printf("%02d : ",i);
for(j=1;j<=mark[i];j++)
printf("*");
printf("\n");
}
}
return 0;
}

19
HDOJ/3044_autoAC.cpp Normal file
View File

@ -0,0 +1,19 @@
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
int main()
{
int T;
double s, l1, l2, v1, v2;
scanf("%d", &T);
while(T--)
{
scanf("%lf%lf%lf%lf", &l1, &l2, &v1, &v2);
s=cos(1)*1.55741*v2*(l1-10)/v1/sin(1);
if(s<0)printf("0\n");
else if(s>l2)printf("%.0lf\n", l2);
else printf("%.0lf\n", s);
}
return 0;
}

55
HDOJ/3045_autoAC.cpp Normal file
View File

@ -0,0 +1,55 @@
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
const int MAXN=400010;
int q[MAXN];
long long sum[MAXN];
long long a[MAXN];
long long dp[MAXN];
long long getDP(int i,int j)
{
return dp[j]+(sum[i]-sum[j])-a[j+1]*(i-j);
}
long long getUP(int j,int k)
{
return dp[j]-sum[j]+j*a[j+1]-(dp[k]-sum[k]+k*a[k+1]);
}
long long getDOWN(int j,int k)
{
return a[j+1]-a[k+1];
}
int main()
{
int n,t;
int head,tail;
while(scanf("%d%d",&n,&t)==2)
{
dp[0]=sum[0]=0;
a[0]=0;
for(int i=1;i<=n;i++)
{
scanf("%I64d",&a[i]);
}
sort(a+1,a+n+1);
for(int i=1;i<=n;i++)
sum[i]=sum[i-1]+a[i];
head=tail=0;
q[tail++]=0;
for(int i=1;i<=n;i++)
{
while(head+1<tail && getUP(q[head+1],q[head])<=i*getDOWN(q[head+1],q[head]))
head++;
dp[i]=getDP(i,q[head]);
int j=i-t+1;
if(j<t)continue;
while(head+1<tail && getUP(j,q[tail-1])*getDOWN(q[tail-1],q[tail-2])<=getUP(q[tail-1],q[tail-2])*getDOWN(j,q[tail-1]))
tail--;
q[tail++]=j;
}
printf("%I64d\n",dp[n]);
}
return 0;
}

138
HDOJ/3046_autoAC.cpp Normal file
View File

@ -0,0 +1,138 @@
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<stack>
#include<cmath>
#include<queue>
using namespace std;
#define CL(x,v); memset(x,v,sizeof(x));
#define INF 0x3f3f3f3f
#define LL long long
#define REP(i,r,n) for(int i=r;i<=n;i++)
#define RREP(i,n,r) for(int i=n;i>=r;i--)
const int MAXN=200*200+100;
struct Edge{
int from,to,cap,flow;
};
bool cmp(const Edge& a,const Edge& b){
return a.from < b.from || (a.from == b.from && a.to < b.to);
}
struct Dinic{
int n,m,s,t;
vector<Edge> edges;
vector<int> G[MAXN];
bool vis[MAXN];
int d[MAXN];
int cur[MAXN];
void init(int n){
this->n=n;
for(int i=0;i<=n;i++)G[i].clear();
edges.clear();
}
void AddEdge(int from,int to,int cap){
edges.push_back((Edge){from,to,cap,0});
edges.push_back((Edge){to,from,cap,0});
m=edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}
bool BFS(){
CL(vis,0);
queue<int> Q;
Q.push(s);
d[s]=0;
vis[s]=1;
while(!Q.empty()){
int x=Q.front();
Q.pop();
for(int i=0;i<G[x].size();i++){
Edge& e=edges[G[x][i]];
if(!vis[e.to]&&e.cap>e.flow){
vis[e.to]=1;
d[e.to]=d[x]+1;
Q.push(e.to);
}
}
}
return vis[t];
}
int DFS(int x,int a){
if(x==t||a==0)return a;
int flow=0,f;
for(int& i=cur[x];i<G[x].size();i++){
Edge& e=edges[G[x][i]];
if(d[x]+1==d[e.to]&&(f=DFS(e.to,min(a,e.cap-e.flow)))>0){
e.flow+=f;
edges[G[x][i]^1].flow-=f;
flow+=f;
a-=f;
if(a==0)break;
}
}
return flow;
}
int Maxflow(int s,int t,int need){
this->s=s;this->t=t;
int flow=0;
while(BFS()){
CL(cur,0);
flow+=DFS(s,INF);
if(flow>need)return flow;
}
return flow;
}
vector<int> Mincut(){
BFS();
vector<int> ans;
for(int i=0;i<edges.size();i++){
Edge& e=edges[i];
if(vis[e.from]&&!vis[e.to]&&e.cap>0)ans.push_back(i);
}
return ans;
}
void Reduce(){
for(int i = 0; i < edges.size(); i++) edges[i].cap -= edges[i].flow;
}
void ClearFlow(){
for(int i = 0; i < edges.size(); i++) edges[i].flow = 0;
}
};
Dinic solver;
int main()
{
int n,m;
int cas=0;
while(~scanf("%d%d",&n,&m))
{
int s=n*m;
int t=n*m+1;
int N=n*m+1;
solver.init(N);
int a;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
scanf("%d",&a);
if(i!=0)
solver.AddEdge((i-1)*m+j,i*m+j,1);
if(j!=0)
solver.AddEdge(i*m+j-1,i*m+j,1);
if(a==1)
solver.AddEdge(s,i*m+j,INF);
if(a==2)
solver.AddEdge(i*m+j,t,INF);
}
}
int ans=solver.Maxflow(s,t,INF);
printf("Case %d:\n",++cas);
printf("%d\n",ans);
}
return 0;
}

42
HDOJ/3047_autoAC.cpp Normal file
View File

@ -0,0 +1,42 @@
#include<cstdio>
#include<cmath>
using namespace std;
#define N 50005
int f[N], rank[N], n, m;
void init(){
for(int i=0; i<=n; ++i)
f[i]=i, rank[i]=0;
}
int find(int x){
if(x==f[x]) return f[x];
int t=f[x];
f[x] = find(f[x]);
rank[x] += rank[t];
return f[x];
}
bool Union(int x,int y, int m){
int a=find(x), b=find(y);
if(a==b){
if(rank[x]+m!=rank[y])
return false;
return true;
}
f[b] = a;
rank[b] = rank[x]+m-rank[y];
return true;
}
int main(){
int a,b,x;
while(~scanf("%d%d",&n,&m)){
init();
int cnt=0;
for(int i=0; i<m; ++i){
scanf("%d%d%d",&a,&b,&x);
if(!Union(a, b, x)){
++cnt;
}
}
printf("%d\n",cnt);
}
return 0;
}

31
HDOJ/3049_autoAC.cpp Normal file
View File

@ -0,0 +1,31 @@
#include<stdio.h>
#include<stdlib.h>
int main()
{
int T,n,x;
__int64 num[40002];
scanf( "%d",&T );
for( int i=1; i<=T; i++ )
{
scanf( "%d",&n );
__int64 ans=0,m=1000003;
m*=n;
num[0]=1;
for( int j=1;j<=40000;j++ )
{
num[ j ]=num[j-1]<<1;
if( num[j]>=m )
num[j]-= m;
}
for( int j=0;j<n; j++ )
{
scanf( "%d",&x );
ans+=num[x];
if( ans >= m )
ans -= m;
}
ans/=n;
printf( "Case %d:%I64d\n",i,ans );
}
return 0;
}

45
HDOJ/3053_autoAC.cpp Normal file
View File

@ -0,0 +1,45 @@
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define MAXD 3010
#define INF 0x3f3f3f3f
int N, P, f[MAXD][MAXD], K[MAXD][MAXD], A[MAXD], a[MAXD];
void init()
{
int i;
scanf("%d%d", &N, &P);
for(i = 1; i <= N; i ++) scanf("%d", &a[i]);
std::sort(a + 1, a + 1 + N);
for(i = 1, A[0] = 0; i <= N; i ++) A[i] = A[i - 1] + a[i];
}
int getw(int x, int y)
{
int t = x + y >> 1;
return (t - x) * a[t] - A[t - 1] + A[x - 1] + A[y] - A[t] - (y - t) * a[t];
}
void solve()
{
int i, j, k, p, t;
for(i = 0; i <= N; i ++) K[i][i] = i, f[i][i] = 0;
for(p = 1; p <= N - P; p ++)
{
for(i = 0; (j = i + p) <= N; i ++) f[i][j] = INF;
for(i = 1; (j = i + p) <= N; i ++)
for(k = K[i][j - 1]; k <= K[i + 1][j]; k ++)
if(f[i][j] > (t = f[i - 1][k - 1] + getw(k, j)))
f[i][j] = t, K[i][j] = k;
}
printf("%d\n", f[P][N]);
}
int main()
{
int t;
scanf("%d", &t);
while(t --)
{
init();
if(P >= N) printf("0\n");
else solve();
}
return 0;
}

34
HDOJ/3054_autoAC.cpp Normal file
View File

@ -0,0 +1,34 @@
#include <cstdio>
int solve( int start , int i) {
int times = 1,s;
if(start == 1) times = 15;
else if(start == 2) times = 150;
else {
times = 750;
start = start - 3;
while(start--) {
times *= 10;
}
}
if(start == 2) {
if( i % 4 == 0)
s = (i + i/4 - 1)*times;
else
s = (i + i/4)*times;
} else {
if( i % 9 == 0)
s = (i + i/9 - 1)*times;
else
s = (i + i/9)*times;
}
return s;
}
int main(void) {
int T,m,k;
scanf("%d",&T);
while(T--) {
scanf("%d %d",&m,&k);
printf("%d\n",solve(m,k));
}
return 0;
}

184
HDOJ/3058_autoAC.cpp Normal file
View File

@ -0,0 +1,184 @@
#include <cstdio>
#include <cstdlib>
#include <climits>
#include <cstring>
#include <cmath>
#include <algorithm>
#include<iostream>
#include<queue>
#include <map>
using namespace std;
typedef long long ll;
#define INF (INT_MAX/10)
#define SQR(x) ((x)*(x))
#define rep(i, n) for (int i=0; i<(n); ++i)
#define repf(i, a, b) for (int i=(a); i<=(b); ++i)
#define repd(i, a, b) for (int i=(a); i>=(b); --i)
#define clr(ar,val) memset(ar, val, sizeof(ar))
#define pb(i) push_back(i)
#define exp 0.000000001
#define N 101
double a[N][N];
int n,m,tot;
char s[20];
bool vis[50];
vector<int>vec;
struct node
{
bool sign;
int len,fal;
int next[26];
};
node ac[10000];
void init()
{
ac[0].sign=false;tot=0;
ac[0].len=0; ac[0].fal=0;
memset(ac[0].next,-1,sizeof(ac[0].next));
memset(a,0,sizeof(a));
memset(vis,false,sizeof(vis));
vec.clear();
}
void tree()
{
int len=strlen(s);
int u=0;
rep(i,len)
{
int x=s[i]-'A';
if(ac[u].next[x]==-1)
{
++tot; ac[tot].sign=false;
memset(ac[tot].next,-1,sizeof(ac[tot].next));
ac[u].next[x]=tot;
}
u=ac[u].next[x];
if(i==len-1) ac[u].sign=true;
}
}
int fail(int u,int k)
{
if(ac[u].next[k]!=-1) return ac[u].next[k];
if(u==0) return 0;
return fail(ac[u].fal,k);
}
void bulid()
{
queue<int>q;
q.push(0);
while(!q.empty())
{
int x=q.front(); q.pop();
rep(i,26)
if(ac[x].next[i]!=-1)
{
q.push(ac[x].next[i]);
if(x==0) ac[ac[x].next[i]].fal=0;
else ac[ac[x].next[i]].fal=fail(ac[x].fal,i);
}
}
int len=0;
q.push(0);
while(!q.empty())
{
int x=q.front(); q.pop();
if(ac[ac[x].fal].sign==true)
ac[x].sign=true;
if(ac[x].sign==false)
ac[x].len=len++;
rep(i,26)
if(ac[x].next[i]!=-1)
q.push(ac[x].next[i]);
}
}
void work()
{
queue<int>q; q.push(0);
int len=vec.size();
int Max=0;
while(!q.empty())
{
int x=q.front(); q.pop();
int l=ac[x].len;
Max=max(Max,l);
a[l][l]-=n;
a[l][0]+=(n-len);
rep(j,len)
{
int u=x; int y=vec[j];
int sign=0;
while(true)
{
if(ac[u].next[y]!=-1)
{
if(ac[ac[u].next[y]].sign==false)
a[l][ac[ac[u].next[y]].len]+=1;
sign=1;
break;
}
if(ac[u].next[y]!=-1) sign=1;
if(u==0) break;
u=ac[u].fal;
}
if(sign==0)
a[l][0]++;
}
rep(i,26)
if(ac[x].next[i]!=-1 && ac[ac[x].next[i]].sign==false)
q.push(ac[x].next[i]);
}
repf(i,0,Max) a[i][Max+1]=n;
n=Max+1,m=Max+2;
}
void guess()
{
rep(i,n)
{
int k=-1;
repf(j,i,n-1)
if(a[j][i]!=0){
k=j; break;
}
if(k==-1) continue;
if(k!=i)
rep(j,m) swap(a[i][j],a[k][j]);
rep(j,n)
{
if(j!=i)
{
double x=a[i][i],y=a[j][i];
rep(k,m)
a[j][k]=a[j][k]-a[i][k]*y/x;
}
}
}
if(fabs(0-a[0][0])<exp) printf("%0.00\n");
else printf("%0.2lf\n",-a[0][m-1]*1.0/a[0][0]);
}
void solve()
{
init();
rep(i,m)
{
scanf("%s",s); tree();
int len=strlen(s);
rep(j,len)
if(vis[s[j]-'A']==false)
{
vec.pb(s[j]-'A'); vis[s[j]-'A']=true;
}
}
bulid();
work();
guess();
}
int main()
{
int test;
scanf("%d",&test);
while(test--)
{
scanf("%d%d",&n,&m);
solve();
}
}

105
HDOJ/3060_autoAC.cpp Normal file
View File

@ -0,0 +1,105 @@
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
const int maxn = 555;
const int maxisn = 10;
const double eps = 1e-8;
const double pi = acos(-1.0);
int dcmp(double x)
{
if(x > eps) return 1;
return x < -eps ? -1 : 0;
}
inline double min(double a, double b)
{return a < b ? a : b;}
inline double max(double a, double b)
{return a > b ? a : b;}
inline double Sqr(double x)
{return x * x;}
struct Point
{
double x, y;
Point(){x = y = 0;}
Point(double a, double b)
{x = a, y = b;}
inline Point operator-(const Point &b)const
{return Point(x - b.x, y - b.y);}
inline Point operator+(const Point &b)const
{return Point(x + b.x, y + b.y);}
inline double dot(const Point &b)const
{return x * b.x + y * b.y;}
inline double cross(const Point &b, const Point &c)const
{return (b.x - x) * (c.y - y) - (c.x - x) * (b.y - y);}
};
Point LineCross(const Point &a, const Point &b, const Point &c, const Point &d)
{
double u = a.cross(b, c), v = b.cross(a, d);
return Point((c.x * v + d.x * u) / (u + v), (c.y * v + d.y * u) / (u + v));
}
double PolygonArea(Point p[], int n)
{
if(n < 3) return 0.0;
double s = p[0].y * (p[n - 1].x - p[1].x);
p[n] = p[0];
for(int i = 1; i < n; ++ i)
s += p[i].y * (p[i - 1].x - p[i + 1].x);
return fabs(s * 0.5);
}
double CPIA(Point a[], Point b[], int na, int nb)//ConvexPolygonIntersectArea
{
Point p[maxisn], tmp[maxisn];
int i, j, tn, sflag, eflag;
a[na] = a[0], b[nb] = b[0];
memcpy(p, b, sizeof(Point) * (nb + 1));
for(i = 0; i < na && nb > 2; ++ i)
{
sflag = dcmp(a[i].cross(a[i + 1], p[0]));
for(j = tn = 0; j < nb; ++ j, sflag = eflag)
{
if(sflag >= 0) tmp[tn ++] = p[j];
eflag = dcmp(a[i].cross(a[i + 1], p[j + 1]));
if((sflag ^ eflag) == -2)
tmp[tn ++] = LineCross(a[i], a[i + 1], p[j], p[j + 1]);
}
memcpy(p, tmp, sizeof(Point) * tn);
nb = tn, p[nb] = p[0];
}
if(nb < 3) return 0.0;
return PolygonArea(p, nb);
}
double SPIA(Point a[], Point b[], int na, int nb)//SimplePolygonIntersectArea
{
int i, j;
Point t1[4], t2[4];
double res = 0, if_clock_t1, if_clock_t2;
a[na] = t1[0] = a[0], b[nb] = t2[0] = b[0];
for(i = 2; i < na; ++ i)
{
t1[1] = a[i - 1], t1[2] = a[i];
if_clock_t1 = dcmp(t1[0].cross(t1[1], t1[2]));
if(if_clock_t1 < 0) std::swap(t1[1], t1[2]);
for(j = 2; j < nb; ++ j)
{
t2[1] = b[j - 1], t2[2] = b[j];
if_clock_t2 = dcmp(t2[0].cross(t2[1], t2[2]));
if(if_clock_t2 < 0) std::swap(t2[1], t2[2]);
res += CPIA(t1, t2, 3, 3) * if_clock_t1 * if_clock_t2;
}
}
return PolygonArea(a, na) + PolygonArea(b, nb) - res;
}
Point p1[maxn], p2[maxn];
int n1, n2;
int main()
{
int i;
while(scanf("%d%d", &n1, &n2) != EOF)
{
for(i = 0; i < n1; ++ i) scanf("%lf%lf", &p1[i].x, &p1[i].y);
for(i = 0; i < n2; ++ i) scanf("%lf%lf", &p2[i].x, &p2[i].y);
printf("%.2f\n", SPIA(p1, p2, n1, n2) + eps);
}
return 0;
}

121
HDOJ/3061_autoAC.cpp Normal file
View File

@ -0,0 +1,121 @@
#include <stdio.h>
#include <string.h>
#define VM 10000
#define EM 120000
#define inf 0x3f3f3f3f
struct E
{
int to,cap,nxt;
}edge[EM];
int head[VM],gap[VM],dist[VM],cur[VM],pre[VM];
int ep;
void addedge (int cu,int cv,int cw)
{
edge[ep].to = cv;
edge[ep].cap = cw;
edge[ep].nxt = head[cu];
head[cu] = ep;
ep ++;
edge[ep].to = cu;
edge[ep].cap = 0;
edge[ep].nxt = head[cv];
head[cv] = ep;
ep ++;
}
int min (int a ,int b)
{
return a > b ? b : a;
}
int sap (int src,int des,int n)
{
memset (dist,0,sizeof(dist));
memset (gap,0,sizeof (dist));
memcpy (cur,head,sizeof(dist));
int res = 0;
int u = pre[src] = src;
int aug = inf;
gap[0] = n;
while (dist[src] < n)
{
loop:
for (int &i = cur[u];i != -1;i = edge[i].nxt)
{
int v = edge[i].to;
if (edge[i].cap && dist[u] == dist[v] + 1)
{
aug = min (aug,edge[i].cap);
pre[v] = u;
u = v;
if (v == des)
{
res += aug;
for (u = pre[u];v != src;v = u,u = pre[u])
{
edge[cur[u]].cap -= aug;
edge[cur[u]^1].cap += aug;
}
aug = inf; //
}
goto loop;
}
}
int mindist = n; //
for (int i = head[u];i != -1;i = edge[i].nxt)
{
int v = edge[i].to;
if (edge[i].cap && mindist > dist[v])
{
cur[u] = i;
mindist = dist[v];
}
}
if ((--gap[dist[u]]) == 0)
break;
dist[u] = mindist + 1;
gap[dist[u]] ++;
u = pre[u];
}
return res;
}
int vis[VM];
void dfs(int u,int v)
{
if(u==v) return ;
vis[u]=1;
for(int i=head[u];i!=-1;i=edge[i].nxt)
if(edge[i].cap>0&&!vis[edge[i].to])
dfs(edge[i].to,v);
}
int main ()
{
int n,m,u,v,p;
int src,des;
int sum;
while (scanf("%d %d",&n,&m)!=EOF)
{
ep = 0;
sum=0;
src = 0;
des = n + 1;
memset (head,-1,sizeof(head));
for(int i=1;i<=n;i++)
{
scanf("%d",&p);
if(p>0)
{
addedge(src,i,p);
sum+=p;
}
else
addedge(i,des,-p);
}
for (int i=1; i<=m; i++)
{
scanf("%d%d",&u,&v);
addedge(u,v,inf);
}
int max_flow=sap(src,des,n + 2);;
printf("%d\n",sum-max_flow);
}
return 0;
}

104
HDOJ/3062_autoAC.cpp Normal file
View File

@ -0,0 +1,104 @@
#include<stdio.h>
#include<string.h>
#include<stack>
using namespace std;
int n,m;
struct edge_node
{
int to;
int next;
};
edge_node edge[5000000];
int head[100000];
int dfn[100000];
int low[100000];
int visit[100000];
int N;
stack<int>sta;
void tarjan_bfs(int cur,int time)
{
visit[cur]=1;
visit[cur^1]=1;
low[cur]=dfn[cur]=time;
sta.push(cur);
for(int i=head[cur];i!=-1;i=edge[i].next)
{
if(visit[edge[i].to]==0)
{
tarjan_bfs(edge[i].to,time++);
time++;
}
if(visit[edge[i].to]==1&&low[cur]>low[edge[i].to])
{
low[cur]=low[edge[i].to];
}
}
if(dfn[cur]==low[cur])
{
N++;
while(!sta.empty()&&sta.top()!=cur)
{
low[sta.top()]=N;
visit[sta.top()]=2;
sta.pop();
}
}
return;
}
void tarjan()
{
N=0;
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(visit,0,sizeof(visit));
for(int i=0;i<2*n;i++)
{
if(visit[i]==0)
{
tarjan_bfs(i,1);
}
}
return;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(head,-1,sizeof(head));
int counter=0;
while(m--)
{
int a1,a2,c1,c2;
scanf("%d%d%d%d",&a1,&a2,&c1,&c2);
int temp1;
int temp2;
temp1=2*a1+c1;
temp2=2*a2+c2;
edge[counter].to=temp2;
edge[counter].next=head[temp1^1];
head[temp1^1]=counter++;
edge[counter].to=temp1;
edge[counter].next=head[temp2^1];
head[temp2^1]=counter++;
}
tarjan();
int sign=1;
for(int i=0;i<2*n;i++)
{
if(low[i]==low[i^1])
{
sign=0;
break;
}
}
if(sign==1)
{
printf("YES\n");
}
else
{
printf("NO\n");
}
}
return 0;
}

29
HDOJ/3063_autoAC.cpp Normal file
View File

@ -0,0 +1,29 @@
#include <iostream>
#include <cstring>
int main()
{
using namespace std;
long long n;
long long sum;
int len;
int i;
int p=4*9999;
char num[110];
while(scanf("%s",num)!=EOF)
{
n=0;
len=strlen(num);
for(i=0; i<len; i++)
{
n=n*10+num[i]-'0';
if(n>p)
{
n=n%p;
}
}
n=n%p;
sum=n*(n+1)*(n+2)*(n+3)%p;
cout<<sum/4<<endl;
}
return 0;
}

143
HDOJ/3065_autoAC.cpp Normal file
View File

@ -0,0 +1,143 @@
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
using namespace std;
char ss[1005][55], T[2000005];
int cnt[1005];
struct Node
{
int flag;
Node *ch[26], *fail;
};
void getstr( char *s )
{
int p = 0;
char c;
while( c = getchar(), c == '\n' ) ;
s[p++] = c;
while( c = getchar(), c != '\n' )
s[p++] = c;
s[p] = '\0';
}
Node *init( )
{
Node *p = new Node;
p->flag = -1;
p->fail = NULL;
memset( p->ch, 0, sizeof( p->ch ) );
return p;
}
void BuildTree( Node *p, char *in, int No )
{
int dx = *in - 'A';
if( *in )
{
if( !p->ch[dx] )
p->ch[dx] = init();
BuildTree( p->ch[dx], in + 1, No );
}
else
p->flag = No;
}
void BuildFailPoint( Node *r )
{
queue<Node *>q;
q.push( r );
while( !q.empty() )
{
Node *pos = q.front();
q.pop();
for( int i = 0; i <26; ++i )
{
Node *f = pos->fail;
if( pos->ch[i] )
{
while( f )
{
if( f->ch[i] )
{
pos->ch[i]->fail = f->ch[i];
break;
}
f = f->fail;
}
if( !f )
{
pos->ch[i]->fail = r;
}
q.push( pos->ch[i] );
}
}
}
}
void AcAuto( Node *r, char *text )
{
int len = strlen( text );
Node *f = r;
for( int i = 0; i < len; ++i )
{
int dx = text[i] - 'A';
if( dx < 0 || dx >= 26 )
{
f = r;
continue;
}
while( f )
{
if( f->ch[dx] )
{
f = f->ch[dx];
break;
}
f = f->fail;
}
if( !f )
f = r;
else
{
Node *t = f;
while( t )
{
if( t->flag!= -1 )
{
cnt[t->flag]++;
}
t = t->fail;
}
}
}
}
void _free( Node *p )
{
for( int i = 0; i < 26; ++i )
{
if( p->ch[i] )
_free( p->ch[i] );
}
free( p );
}
int main()
{
int N;
while( scanf( "%d", &N ) != EOF )
{
Node *r = init();
memset( cnt, 0, sizeof( cnt ) );
for( int i = 0; i < N; ++i )
{
getstr( ss[i] );
BuildTree( r, ss[i], i );
}
BuildFailPoint( r );
getstr( T );
AcAuto( r, T );
for( int i = 0; i < N; ++i )
{
if( cnt[i] )
printf( "%s: %d\n", ss[i], cnt[i] );
}
_free( r );
}
return 0;
}

27
HDOJ/3067_autoAC.cpp Normal file
View File

@ -0,0 +1,27 @@
#include <stdio.h>
int main()
{
int i,a,n,sum,ans;
while(~scanf("%d",&n))
{
for(i = 0,sum = 0; i<n; i++,sum+=a)
scanf("%d",&a);
for(i = 1; i<sum; i++)
{
if(sum==(1+i)*i/2)
{
ans = 1;
break;
}
if(sum<(1+i)*i/2)
{
ans = i;
break;
}
}
if(sum == 2)
ans = 2;
printf("yes\n%d\n",ans);
}
return 0;
}

50
HDOJ/3068_autoAC.cpp Normal file
View File

@ -0,0 +1,50 @@
#include <iostream>
#include <cstring>
#include <string>
#include <map>
#include <queue>
#include <algorithm>
using namespace std;
char s[211000],c[111000];
int p[211000];
void init()
{
int i,j;
s[0]='@';
for(i=0;c[i]!=0;i++)
{
s[2*i+1]='#';
s[2*i+2]=c[i];
}
s[2*i+1]='#';
s[2*i+2]=0;
}
int manacher()
{
int id=0,mx=0,i;
for(i=1;s[i]!=0;i++)
{
p[i]=mx>i?min(p[2*id-i],mx-i):1;
while(s[i+p[i]] == s[i-p[i]])p[i]++;
if(i+p[i]>mx)
{
mx=i+p[i];
id=i;
}
}
mx=0;
for(i=1;s[i]!=0;i++)
{
if(p[i]>mx)mx=p[i];
}
return mx-1;
}
int main()
{
while(~scanf("%s",c))
{
init();
printf("%d\n",manacher());
}
return 0;
}

51
HDOJ/3069_autoAC.cpp Normal file
View File

@ -0,0 +1,51 @@
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
#define maxn 1005
int head[maxn],tot,u,v,n;
struct Edge
{
int to,next;
}e[maxn*2];
void addedge(int u,int v)
{
e[tot].to=v;
e[tot].next=head[u];
head[u]=tot++;
}
int dfs(int u,int pre)
{
int ans=-1,cnt=0;
vector<int> check;
for(int i=head[u];i!=-1;i=e[i].next)
{
int v=e[i].to,t;
if(v==pre) continue;
t=dfs(v,u);
check.push_back(t);
ans=max(ans,t);
}
if(check.size()==0) return 1;
for(int i=0;i<check.size();i++) if(check[i]==ans) cnt++;
if(cnt>1) return ans+1;
else return ans;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
tot=0;
memset(head,-1,sizeof(head));
for(int i=0;i<n-1;i++)
{
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
}
int ans=dfs(1,0);
printf("%d\n",ans);
}
}

57
HDOJ/3070_autoAC.cpp Normal file
View File

@ -0,0 +1,57 @@
#include<iostream>
#include<string.h>
using namespace std;
#define MAXN 150
#define MOD 20090829
__int64 dp[MAXN][MAXN],c[MAXN][MAXN];
__int64 a[MAXN];
__int64 b[MAXN];
int gcd(int x,int y)
{
if (y==0) return x;
else return gcd(y,x%y);
}
__int64 C(int k,int n)
{
int i,j;
if (c[k][n]!=0) return c[k][n];
__int64 temp,ans;
for(i=n-k+1;i<=n;i++) a[i]=i;
for(i=1;i<=k;i++) b[i]=i;
for(i=1;i<=k;i++)
for(j=n-k+1;j<=n;j++)
{
temp=gcd(b[i],a[j]);
b[i]/=temp;
a[j]/=temp;
}
ans=1;
for(i=n-k+1;i<=n;i++)
ans=ans*a[i]%MOD;
c[k][n]=ans;
return ans;
}
int min(int x,int y)
{
if (x<y) return x;
else return y;
}
int main()
{
int n,d,i,j,k;
__int64 ans;
memset(c,0,sizeof(c));
while(scanf("%d %d",&n,&d)!=EOF)
{
memset(dp,0,sizeof(dp));
dp[0][0]=1;
for(i=1;i<=n;i++)
for(j=0;j<=n-2;j++)
for(k=0;k<=min(j,d-1);k++)
{
dp[i][j]=(dp[i][j]+dp[i-1][j-k]*C(k,n-2-(j-k)))%MOD;
}
ans=dp[n][n-2];
printf("%I64d\n",ans);
}
}

106
HDOJ/3071_autoAC.cpp Normal file
View File

@ -0,0 +1,106 @@
#include<cstdio>
const int maxn=444444;
int prime[]={ 2, 3, 5, 7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97};
int dpos[]={28,25,23,21,20,19,18,17,16,15,14,13,12,11,10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
int a[]={1,2,4,8,16,32,64};
int b[]={1,3,9,27,81};
int c[]={1,5,25};
int d[]={1,7,49};
#define lson l,mid,lrt
#define rson mid+1,r,rrt
#define mid ((l+r)>>1)
#define lrt rt<<1
#define rrt rt<<1|1
int MAX[maxn],MIN[maxn];
inline int turn(int x){
int cnt,y=0;
for(int i=0;i<25&&x>1;i++){
for(cnt=0;x%prime[i]==0;x/=prime[i]) cnt++;
y|=cnt<<dpos[i];
}
return y;
}
inline int back(int x,int p)
{
long long y=1;
int k=x>>dpos[0];y=y*a[k]%p;x^=k<<dpos[0];
k=x>>dpos[1];y=y*b[k]%p;x^=k<<dpos[1];
k=x>>dpos[2];y=y*c[k]%p;x^=k<<dpos[2];
k=x>>dpos[3];y=y*d[k]%p;x^=k<<dpos[3];
for(int i=4;i<25;i++)
if(x&(1<<dpos[i])) y=y*prime[i]%p;
return y;
}
#define _min(x,y) ((x)<(y)?(x):(y))
#define _max(x,y) ((x)>(y)?(x):(y))
inline int min(int x,int y){
return _min(x&0x70000000,y&0x70000000)|_min(x&0x0e000000,y&0x0e000000)|_min(x&0x01800000,y&0x01800000)|_min(x&0x00600000,y&0x00600000)|((x&0x001fffff)&(y&0x001fffff));
}
inline int max(int x,int y){
return _max(x&0x70000000,y&0x70000000)|_max(x&0x0e000000,y&0x0e000000)|_max(x&0x01800000,y&0x01800000)|_max(x&0x00600000,y&0x00600000)|((x&0x001fffff)|(y&0x001fffff));
}
inline void pushup(int rt){
MAX[rt]=max(MAX[lrt],MAX[rrt]);
MIN[rt]=min(MIN[lrt],MIN[rrt]);
}
void build(int l,int r,int rt){
if(l==r){
int x;scanf("%d",&x);
MIN[rt]=MAX[rt]=turn(x);
return;
}
build(lson);build(rson);
pushup(rt);
}
void update(int k,int x,int l,int r,int rt){
if(l==r){
MAX[rt]=MIN[rt]=turn(x);
return;
}
if(k<=mid) update(k,x,lson);
else update(k,x,rson);
pushup(rt);
}
int query_max(int s,int t,int l,int r,int rt){
if(s<=l&&t>=r) return MAX[rt];
int ret=0;
if(s<=mid) ret=max(ret,query_max(s,t,lson));
if(t>mid) ret=max(ret,query_max(s,t,rson));
return ret;
}
int query_min(int s,int t,int l,int r,int rt){
if(s<=l&&t>=r) return MIN[rt];
int ret=0x7fffffff;
if(s<=mid) ret=min(ret,query_min(s,t,lson));
if(t>mid) ret=min(ret,query_min(s,t,rson));
return ret;
}
int main()
{
int n,q;
while(scanf("%d%d",&n,&q)!=EOF){
build(1,n,1);
char s[2];
while(q--){
scanf("%s",s);
if(s[0]=='C'){
int k,v;
scanf("%d%d",&k,&v);
update(k,v,1,n,1);
}
else if(s[0]=='L'){
int k1,k2,p;
scanf("%d%d%d",&k1,&k2,&p);
int x=query_max(k1,k2,1,n,1);
printf("%u\n",back(x,p));
}
else{
int k1,k2,p;
scanf("%d%d%d",&k1,&k2,&p);
int x=query_min(k1,k2,1,n,1);
printf("%u\n",back(x,p));
}
}
}
return 0;
}

211
HDOJ/3072_autoAC.cpp Normal file
View File

@ -0,0 +1,211 @@
#include<iostream>
#include<cstring>
#include<stack>
#include<cstdio>
#define INF 0x7ffffff
#define mm(x) memset(x,0,sizeof(x))
using namespace std;
const int N=50000+9;
const int M=100000+9;
stack<int>s;
struct node
{
int to,next;
int w;
}G[M];
struct edge
{
int u,v,w;
}NG[M];
int indegree[N];
int instack[N],paint[N],col,index,dfn[N],low[N];
int head[N],cnt,Ncnt;
class tarjan
{
public:
int MIN(int a,int b)
{
if(a<b) return a;
return b;
}
void Init()
{
while(!s.empty()) s.pop();
mm(instack);
mm(paint);
mm(dfn);
mm(low);
mm(indegree);
index=1;
col=0;
memset(head,-1,sizeof(head));
cnt=0;
Ncnt=0;
}
void add(int a,int b,int c)
{
G[cnt].to=b;
G[cnt].w=c;
G[cnt].next=head[a];
head[a]=cnt++;
}
void Nadd(int a,int b,int c)
{
NG[Ncnt].u=a;
NG[Ncnt].v=b;
NG[Ncnt++].w=c;
}
void Tarjan(int u)
{
dfn[u]=low[u]=index++;
instack[u]=1;
s.push(u);
for(int i=head[u];i+1;i=G[i].next)
{
int v=G[i].to;
if(!dfn[v])
{
Tarjan(v);
low[u]=MIN(low[u],low[v]);
}
else if(instack[v])
{
low[u]=MIN(low[u],dfn[v]);
}
}
if(low[u]==dfn[u])
{
int k=s.top();
while(k!=u)
{
s.pop();
paint[k]=col;
instack[k]=0;
k=s.top();
}
s.pop();
paint[u]=col;
instack[u]=0;
col++;
}
}
void build_NG(int n)
{
for(int i=0;i<n;i++)
{
for(int j=head[i];j+1;j=G[j].next)
{
int u=i,v=G[j].to;
if(paint[u]!=paint[v])
{
Nadd(paint[u],paint[v],G[j].w);
}
}
}
}
}pre_deal;
__int64 directed_mst(int root,int nv,int ne)
{
__int64 res=0;
int in[N],pre[N];
int id[N],vis[N];
while(true)
{
for(int i=0;i<nv;i++)
{
in[i]=INF;
}
for(int i=0;i<ne;i++)
{
int u=NG[i].u;
int v=NG[i].v;
if(NG[i].w<in[v]&&u!=v)
{
pre[v]=u;
in[v]=NG[i].w;
}
}
for(int i=0;i<nv;i++)
{
if(i==root) continue;
if(in[i]==INF) return -1;
}
int cntnode=0;
in[root]=0;
memset(id,-1,sizeof(id));
memset(vis,-1,sizeof(vis));
for(int i=0;i<nv;i++)
{
int v=i;
res+=in[i];
while(vis[v]!=i&&id[v]==-1&&v!=root)
{
vis[v]=i;
v=pre[v];
}
if(id[v]==-1&&v!=root)
{
for(int u=pre[v];u!=v;u=pre[u])
{
id[u]=cntnode;
}
id[v]=cntnode++;
}
}
if(cntnode==0) break;
for(int i=0;i<nv;i++)
{
if(id[i]==-1)
{
id[i]=cntnode++;
}
}
for(int i=0;i<ne;i++)
{
int v=NG[i].v;
NG[i].u=id[NG[i].u];
NG[i].v=id[NG[i].v];
if(NG[i].u!=NG[i].v)
{
NG[i].w-=in[v];
}
}
nv=cntnode;
root=id[root];
}
return res;
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
int root;
pre_deal.Init();
while(m--)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
indegree[b]++;
pre_deal.add(a,b,c);
}
for(int i=0;i<n;i++)
{
if(!dfn[i])
pre_deal.Tarjan(i);
}
int nv=col--;
pre_deal.build_NG(n);
for(int i=0;i<n;i++)
{
if(indegree[i]==0)
{
root=i;
break;
}
}
__int64 ans=directed_mst(paint[root],nv,Ncnt);
printf("%I64d\n",ans);
}
return 0;
}

83
HDOJ/3074_autoAC.cpp Normal file
View File

@ -0,0 +1,83 @@
#include <iostream>
#include <algorithm>
using namespace std;
#define N 50005
const int MOD = 1000000007;
#define LL(x) ((x)<<1)
#define RR(x) ((x)<<1|1)
struct Seg_Tree
{
int left,right,mid;
__int64 num;
}bt[3*N];
__int64 num[N];
void build(int l,int r,int nid)
{
bt[nid].left=l;
bt[nid].right=r;
bt[nid].mid=(l+r)>>1;
if(l!=r)
{
build(l,bt[nid].mid,LL(nid));
build(bt[nid].mid+1,r,RR(nid));
bt[nid].num=(bt[LL(nid)].num%MOD)*(bt[RR(nid)].num%MOD)%MOD;
}
else
bt[nid].num=num[l];
}
void update(int id,int x,int nid)
{
if(bt[nid].left==bt[nid].right)
{
bt[nid].num=x;
return ;
}
if(id<=bt[nid].mid)
update(id,x,LL(nid));
else
update(id,x,RR(nid));
bt[nid].num=(bt[2*nid].num*bt[2*nid+1].num)%MOD;
}
__int64 query(int l,int r,int nid)
{
if(l==bt[nid].left&&r==bt[nid].right)
return bt[nid].num%MOD;
if(r<=bt[nid].mid)
return query(l,r,LL(nid))%MOD;
else if(l>bt[nid].mid)
return query(l,r,RR(nid))%MOD;
else
return (query(l,bt[nid].mid,LL(nid))%MOD)*(query(bt[nid].mid+1,r,RR(nid))%MOD)%MOD;
}
int main()
{
int T,i,j;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
for(i = 1 ; i <= n; i ++)
scanf("%I64d",&num[i]);
build(1,n,1);
int cas,oper,a,b;
scanf("%d",&cas);
while(cas--)
{
scanf("%d",&oper);
scanf("%d%d",&a,&b);
switch(oper)
{
case 0:
printf("%I64d\n",query(a,b,1));
break;
case 1:
update(a,b,1);
break;
default:
break;
}
}
}
return 0;
}

55
HDOJ/3076_autoAC.cpp Normal file
View File

@ -0,0 +1,55 @@
#include<iostream>
using namespace std;
#define N 2005
double a[7],b[7],dp[N][N];
double pa,pb,ptie;
int hp1,hp2;
int main()
{
int i,j;
while(scanf("%d%d",&hp1,&hp2)!=EOF)
{
swap(hp1,hp2);
for(i=1;i<=6;i++)
scanf("%lf",&a[i]);
for(i=1;i<=6;i++)
scanf("%lf",&b[i]);
pa=pb=0;
for(i=2;i<=6;i++)
{
for(j=1;j<=i-1;j++)
{
pa+=a[i]*b[j];
pb+=a[j]*b[i];
}
}
ptie=1-pa-pb;
if(ptie!=1)
{
pa=pa/(1-ptie);
pb=pb/(1-ptie);
}
//dp[i][j]->probability of A won i,B won j
dp[0][0]=1;
for(i=0;i<hp2;i++)
{
for(j=0;j<=hp1;j++)
{
if(i||j)
{
dp[i][j]=0;
if(i)
dp[i][j]+=dp[i-1][j]*pa;
if(j)
dp[i][j]+=dp[i][j-1]*pb;
}
}
}
double ans=0;
for(i=0;i<hp1;i++)
ans+=dp[hp2-1][i]*pa;
if(ans>1)
ans=1;
printf("%.6lf\n",ans);
}
}

118
HDOJ/3078_autoAC.cpp Normal file
View File

@ -0,0 +1,118 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
#define N 80010
int __pow[25];
int fa[N],val[N],p[N];
int node[2*N],first[N],dep[2*N],dp[2*N][25];
bool vis[N];
vector<int>e[N];
void dfs(int &index , int u ,int d , int par)
{
++index; vis[u] = true;
first[u] = index; node[index] = u; dep[index] = d; fa[u] = par;
for(int i=0; i<e[u].size(); i++)
if(!vis[e[u][i]])
{
dfs(index , e[u][i] , d+1 , u);
++index;
node[index] = u; dep[index] = d;
}
}
void ST(int n)
{
int K = (int)(log((double)n) / log(2.0));
for(int i=1; i<=n; i++) dp[i][0] = i;
for(int j=1; j<=K; j++)
for(int i=1; i+__pow[j]-1 <= n ; i++)
{
int a = dp[i][j-1];
int b = dp[i+__pow[j-1]][j-1];
if(dep[a] < dep[b]) dp[i][j] = a;
else dp[i][j] = b;
}
}
int RMQ(int x ,int y)
{
int K = (int)(log((double)(y-x+1)) / log(2.0));
int a = dp[x][K];
int b = dp[y-__pow[K]+1][K];
if(dep[a] < dep[b]) return a;
else return b;
}
int LCA(int u ,int v)
{
int x = first[u];
int y = first[v];
if(x > y) swap(x,y);
int index = RMQ(x,y);
return node[index];
}
bool cmp(int a, int b)
{
return a > b;
}
void path(int &index , int s , int t)
{
while(s != t)
{
p[index++] = val[s];
s = fa[s];
}
p[index++] = val[t];
}
void solve(int kth , int u,int v)
{
int lca = LCA(u,v);
int tot = 0;
path(tot,u,lca);
path(tot,v,lca);
tot--;
if(kth > tot)
{
printf("invalid request!\n");
return ;
}
sort(p,p+tot,cmp);
printf("%d\n",p[kth-1]);
}
int main()
{
for(int i=0; i<25; i++) __pow[i] = 1 << i;
int n,q;
scanf("%d%d",&n,&q);
for(int i=1; i<=n; i++) scanf("%d",&val[i]);
for(int i=1; i<n; i++)
{
int u,v;
scanf("%d%d",&u,&v);
e[u].push_back(v);
e[v].push_back(u);
}
int tot = 0;
memset(vis,false,sizeof(vis));
dfs(tot,1,1,-1);
ST(tot);
while(q--)
{
int op;
scanf("%d",&op);
if(op == 0)
{
int x,w;
scanf("%d%d",&x,&w);
val[x] = w;
}
else
{
int u,v;
scanf("%d%d",&u,&v);
solve(op,u,v);
}
}
return 0;
}

24
HDOJ/3079_autoAC.cpp Normal file
View File

@ -0,0 +1,24 @@
#include<stdio.h>
#include<string.h>
int main()
{
int i,j,t;
char str[55];
while(scanf("%d",&t)!=EOF)
{
for(i=0;i<t;i++)
{
scanf("%s",str);
for(j=0;str[j]!='\0';j++)
{
if(str[j]=='a' || str[j]=='e' || str[j]=='i' || str[j]=='o' || str[j]=='u')
str[j]-=32;
else
if(str[j]!='A' && str[j]!='E' && str[j]!='I' && str[j]!='O' && str[j]!='U' && str[j]>='A' && str[j]<='Z')
str[j]+=32;
}
printf("%s\n",str);
}
}
return 0;
}

123
HDOJ/3080_autoAC.cpp Normal file
View File

@ -0,0 +1,123 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int MAX=210;
int n,cnt,ret;
int p[MAX],ran[MAX];
bool ext[MAX];
struct Edge
{
int from;
int to;
int weight;
bool operator<(const Edge& edge) const
{
if(ext[from]==false||ext[to]==false)
return false;
if(ext[edge.from]==false||ext[edge.to]==false)
return true;
return weight<edge.weight;
}
}edge[MAX*MAX];
void Init()
{
for(int i=0;i<210;i++)
{
p[i]=i;
ran[i]=0;
}
}
void Link(int x,int y)
{
if(ran[x]<ran[y])
p[x]=y;
else
{
p[y]=x;
if(ran[x]==ran[y])
ran[x]++;
}
}
int Find(int x)
{
if(p[x]!=x)
p[x]=Find(p[x]);
return p[x];
}
void Union(int x,int y)
{
Link(Find(x),Find(y));
}
bool Kruskal()
{
sort(edge,edge+cnt);
int done=0;
Init();
for(int i=0;i<cnt&&done<n-1;i++)
{
if(ext[edge[i].from]==false||ext[edge[i].to]==false)
break;
if(Find(edge[i].from)!=Find(edge[i].to))
{
done++;
ret+=edge[i].weight;
Union(edge[i].from,edge[i].to);
}
}
if(done==n-1)
return true;
else
return false;
}
int main()
{
int t;
int x,y,w;
int nn,ee,m;
scanf("%d",&t);
while(t--)
{
cnt=0;
ret=0;
memset(ext,0,sizeof(ext));
scanf("%d%d",&nn,&ee);
n=nn;
for(int i=0;i<nn;i++)
ext[i]=true;
for(int i=0;i<ee;i++)
{
scanf("%d%d%D",&x,&y,&w);
edge[cnt].from=x;
edge[cnt].to=y;
edge[cnt].weight=w;
cnt++;
}
scanf("%d%d",&nn,&ee);
for(int i=n;i<n+nn;i++)
ext[i]=true;
n+=nn;
for(int i=0;i<ee;i++)
{
scanf("%d%d%D",&x,&y,&w);
edge[cnt].from=x;
edge[cnt].to=y;
edge[cnt].weight=w;
cnt++;
}
scanf("%d",&m);
n-=m;
for(int i=0;i<m;i++)
{
scanf("%d",&x);
ext[x]=false;
}
if(Kruskal())
printf("%d\n",ret);
else
printf("what a pity!\n");
}
return 0;
}

161
HDOJ/3081_autoAC.cpp Normal file
View File

@ -0,0 +1,161 @@
#include <iostream>
#include <algorithm>
using namespace std;
int mp[210][210];
int m[210][210];
int set[410];
int N, M, f, sum, ans;
const int inf = 0x7f7f7f7f;
bool Find;
int vh[410];
int h[410];
int find( int x)
{
return x == set[x] ? x : set[x] = find( set[x] );
}
void init_flow( )
{
memset(h, 0, sizeof(h));
memset(vh, 0, sizeof(vh));
}
void init( )
{
for( int i = 0; i < 110; i++)
set[i] = i;
memset(mp, 0, sizeof(mp));
memset(m, 0, sizeof(m));
}
//婧0锛姹N + 1
void make_graph(int mid)
{
//婧瑰板濂崇
memset(mp, 0, sizeof(mp));
for( int i = 1; i <= N; i++)
{
mp[0][i] = mid;
}
//风版
for( int i = N + 1; i <= 2 * N; i++)
{
mp[i][2 * N + 1] = mid;
}
for( int i = 1; i <= 2 * N; i++)
for( int j = 1; j <= 2 * N; j++)
mp[i][j] = m[i][j];
//崇郴浠ヤ
for( int i = 1; i <= 2 * N; i++)
{
for( int j = 1; j <= 2 * N; j++)
{
if( set[i] == set[j])
{
for( int k = 1; k <= 2 * N; k++)
{
if ( mp[j][k] && k != i)
mp[i][k] = 1;
}
}
}
}
}
void dfs( int x )
{
int hmin = 2 * N + 1, i;
if( x == 2 * N + 1)
{
sum += ans;
Find = true;
return;
}
for( i = 0; i <= 2 * N + 1; i++)
{
if( mp[x][i] > 0)
{
if( h[x] == h[i] + 1 )
{
if( mp[x][i] < ans )
ans = mp[x][i];
dfs( i );
if( Find )
break;
if( h[0] >= 2 * N + 1 )
return;
}
if( h[i] < hmin )
hmin = h[i];
}
}
if( Find )
{
mp[x][i] -= ans;
mp[i][x] += ans;
}
else
{
vh[h[x]]--;
if( vh[h[x]] == 0 )
h[0] = 2 * N + 1;
h[x] = hmin + 1;
vh[h[x]]++;
}
}
int sap( )
{
sum = 0;
init_flow( );
vh[0] = 2 * N + 2;
while( h[0] < 2 * N + 2 )
{
ans = inf;
Find = false;
dfs( 0 );
}
return sum;
}
int solve( )
{
int l = 0, r = N, temp = 0;
while( l <= r )
{
int mid = (l + r) / 2;
make_graph(mid);
if( sap( ) == mid * N )
{
temp = mid;
l = mid + 1;
}
else
r = mid - 1;
}
return temp;
}
int main( )
{
int T, a, b;
cin>>T;
while( T-- )
{
cin>>N>>M>>f;
init( );
for( int i = 1; i <= M; i++)
{
cin>>a>>b;
mp[a][b + N] = 1;
m[a][b + N] = 1;
}
for( int i = 1; i <= f; i++)
{
cin>>a>>b;
int x = find( a );
int y = find( b );
if( x != y )
set[x] = y;
}
for( int i = 1; i <= N; i++)
{
set[i] = find( i );
}
printf("%d\n", solve( ));
}
return 0;
}

43
HDOJ/3082_autoAC.cpp Normal file
View File

@ -0,0 +1,43 @@
#include <iostream>
using namespace std;
int main()
{
int t;
cin >> t;
while(t--)
{
char buf[120];
int n;
cin >> n;
int dz[22];
for(int j = 0; j < n; j++)
{
int cnt = 0;
int a=0, sum = 0;
getchar();
gets(buf);
int len = strlen(buf);
for(int i = 0; i < len; i++)
{
if(isdigit(buf[i]))
{
a = a * 10 + buf[i] - '0';
}
else
{
sum += a;
a = 0;
}
}
dz[j] = sum;
}
double result = 0;
for(int i = 0 ; i < n; i++)
{
result += 1.0 / dz[i];
}
result = 1.0 / result;
printf("%.2lf\n", result);
}
return 0;
}

73
HDOJ/3083_autoAC.cpp Normal file
View File

@ -0,0 +1,73 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <functional>
#include <numeric>
#include <cctype>
using namespace std;
const int MOD = 20091226;
typedef long long LL;
inline int getS(LL n) {
LL mod = MOD * 6;
LL ret = (n % mod) * ((n + 1) % mod);
ret %= mod;
ret *= (n + 2) % mod;
ret %= mod;
ret /= 6;
return (int)ret;
}
inline int getX(LL n) {
if(n <= 1) {
return 0;
}
int mod = MOD * 24;
LL ret = (n + 1) % mod;
if(n % 2 == 1) {
ret = (((2 * ret - 3) * ret) % mod - 2) * ret;
}else {
ret = (n % mod) * ((n + 2) % mod);
ret %= mod;
ret *= (2 * n - 1) % mod;
}
ret %= mod;
ret /= 24;
return ret;
}
inline int getP(LL n) {
if(n < 1) {
return 0;
}
int mod = MOD * 8;
LL ret = n % mod;
ret = ((((ret * (ret + 1))% mod) * (ret + 2))% mod) * (ret + 3);
ret %= mod;
ret /= 8;
return ret;
}
int main() {
int n;
LL t, d, p, s, x;
while(scanf("%d", &n) == 1) {
s = getS(n);
x = getX(n);
t = s + x;
d = 3 * x;
p = getP(n - 1);
printf("Triangle: %d\n", t % MOD);
printf("Diamond: %d\n", d % MOD);
printf("Parallelogram: %d\n", p % MOD);
}
return 0;
}

107
HDOJ/3085_autoAC.cpp Normal file
View File

@ -0,0 +1,107 @@
#include<stdio.h>
#include<string.h>
#include<string>
#include<queue>
#include<map>
#include<iostream>
#include<algorithm>
#define M 800
using namespace std;
struct node
{
int x,y;
node(){}
node(int xx,int yy)
{
x=xx;
y=yy;
}
}gg,mm,zz[2],f[2][M*M];
int step,front[2],rear[2];
char ma[M][M];
int n,m;
int dir[4][2]={{-1,0},{0,1},{0,-1},{1,0}};
bool maphaten(int x,int y)
{
if(x>=0&&x<n&&y>=0&&y<m&&ma[x][y]!='X')
{
for(int i=0;i<2;i++)
{
if(abs(x-zz[i].x)+abs(y-zz[i].y)<=2*step) return false;
}
return true;
}
return false;
}
queue<node>q[3];
void clear()
{
for(int i=0;i<3;i++)
while(!q[i].empty())
q[i].pop();
}
bool bfs(int num,int move_step,char s,char e)
{
q[2]=q[num];
for(int i=0;i<move_step;i++)
{
node u,v;
while(!q[2].empty())
{
u=q[2].front();
q[2].pop();
q[num].pop();
if(!maphaten(u.x,u.y)) continue;
for(int k=0;k<4;k++)
{
v=u;
v.x=u.x+dir[k][0];
v.y=u.y+dir[k][1];
if(!maphaten(v.x,v.y)||ma[v.x][v.y]==s) continue;
if(ma[v.x][v.y]==e) return true;
ma[v.x][v.y]=s;
q[num].push(v);
}
}
q[2]=q[num];
}
return false;
}
int BFS()
{
step=0;
clear();
q[0].push(mm);
q[1].push(gg);
while(!q[0].empty()&&!q[1].empty())
{
step++;
if(bfs(0,3,'M','G')||bfs(1,1,'G','M')) return step;
}
return -1;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
int k=0;
for(int i=0;i<n;i++)
{
scanf("%s",ma[i]);
for(int j=0;j<m;j++)
{
if(ma[i][j]=='M')
mm=node(i,j);
else if(ma[i][j]=='G')
gg=node(i,j);
else if(ma[i][j]=='Z')
zz[k++]=node(i,j);
}
}
printf("%d\n",BFS());
}
return 0;
}

58
HDOJ/3086_autoAC.cpp Normal file
View File

@ -0,0 +1,58 @@
#include<iostream>
#include<cmath>
using namespace std;
const double eps = 1e-8;
int main()
{
int t;
double s, ax, bx, ay, by;
double a, b, c, p, ans1, ans2;
scanf("%d", &t);
while(t --)
{
scanf("%lf%lf%lf%lf%lf", &s, &ax, &bx, &ay, &by);
a = (ax - ay) / 2;
b = bx - by;
c = s;
if(fabs(a) < eps)
{
if(b > eps)
{
printf("Good driver,Jiaoshou!\n");
}
else if(fabs(b) < eps)
{
printf("Good driver,Jiaoshou!\n");
}
else
{
printf("Oh,God!Jiaoshou will be catched after %.3f minutes.\n", -s / b);
}
}
else{
p = b * b - 4 * a * c;
if(p < -eps)
{
printf("Good driver,Jiaoshou!\n");
}
else
{
ans1 = (-b - sqrt(p)) / (2 * a);
ans2 = (-b + sqrt(p)) / (2 * a);
if(ans1 < 0 && ans2 < 0)
{
printf("Good driver,Jiaoshou!\n");
}
else if(ans1 > 0)
{
printf("Oh,God!Jiaoshou will be catched after %.3f minutes.\n", ans1);
}
else
{
printf("Oh,God!Jiaoshou will be catched after %.3f minutes.\n", ans2);
}
}
}
}
return 0;
}

98
HDOJ/3088_autoAC.cpp Normal file
View File

@ -0,0 +1,98 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
char str[15];
int map[15];
int len;
int vis[3][3][3][3][3][3][3][3][3][3];
int cor[3];
int flag;
struct node
{
int m[15];
int step;
}now,temp;
void bfs()
{
queue<node> q;
for(int i=0; i<10; i++)
{
now.m[i] = map[i];
}
vis[map[0]][map[1]][map[2]][map[3]][map[4]][map[5]][map[6]][map[7]][map[8]][map[9]] = 1;
now.step = 0;
q.push(now);
while(!q.empty())
{
now = q.front();
q.pop();
int k;
for(k=0; k<len-1; k++)
{
if(now.m[k] != now.m[k+1])
break;
}
if(k >= len-1)
{
cout<<now.step<<endl;
flag = 1;
return;
}
for(int i=0; i<len-1; i++)
{
cor[0] = cor[1] = cor[2] = 0;
temp = now;
if(temp.m[i] != temp.m[i+1])
{
cor[temp.m[i]] = 1;
cor[temp.m[i+1]] = 1;
for(int j=0; j<=2; j++)
{
if(!cor[j])
{
temp.m[i] = temp.m[i+1] = j;
break;
}
}
if(!vis[temp.m[0]][temp.m[1]][temp.m[2]][temp.m[3]][temp.m[4]][temp.m[5]][temp.m[6]][temp.m[7]][temp.m[8]][temp.m[9]])
{
temp.step += 1;
vis[temp.m[0]][temp.m[1]][temp.m[2]][temp.m[3]][temp.m[4]][temp.m[5]][temp.m[6]][temp.m[7]][temp.m[8]][temp.m[9]] = 1;
q.push(temp);
}
}
}
}
}
int main()
{
int t;
string a;
scanf("%d",&t);
while(t--)
{
memset(vis, 0, sizeof(vis));
scanf("%s",str);
len = strlen(str);
for(int i=0; i<10; i++)
{
if(i > len)
{
map[i] = 3;
}
if(str[i] == 'r')
map[i] = 0;
else if(str[i] == 'g')
map[i] = 1;
else
map[i] = 2;
}
flag = 0;
bfs();
if(!flag)
cout<<"No solution!"<<endl;
}
return 0;
}

35
HDOJ/3089_autoAC.cpp Normal file
View File

@ -0,0 +1,35 @@
#include <cstdio>
#define LL long long
LL solve(LL n,LL k,LL s=1)
{
if(k==1) return (n-1+s)%n;
LL ans=0;
for(LL i=2;i<=n;)
{
if(ans+k<i)
{
LL leap;
if((i-ans-1)%(k-1)==0) leap=(i-ans-1)/(k-1)-1;
else leap=(i-ans-1)/(k-1);
if(i+leap>n) return ((ans+(n+1-i)*k)+s)%n;
i+=leap;
ans+=leap*k;
}
else
{
ans=(ans+k)%i;
i++;
}
}
return (ans+s)%n;
}
int main()
{
LL n,k;
while(scanf("%I64d%I64d",&n,&k)!=EOF)
{
LL ans=solve(n,k);
if(ans==0) printf("%I64d\n",n);
else printf("%I64d\n",ans);
}
}

42
HDOJ/3090_autoAC.cpp Normal file
View File

@ -0,0 +1,42 @@
#include <cstdio>
#include <algorithm>
using namespace std;
struct Node
{
int d,p;
};
Node roads[10001];
int n,m;
int sum;
bool cmp(Node n1,Node n2)
{
return n1.p > n2.p;
}
int main()
{
while(scanf("%d %d", &n, &m), m!=0 || n!=0)
{
sum = 0;
for(int i=0; i<n; i++)
{
scanf("%d%d", &roads[i].d, &roads[i].p);
sum += roads[i].d*roads[i].p;
}
sort(roads, roads+n, cmp);
for(int i=0; i<n; i++)
{
if(m > roads[i].d)
{
m -= roads[i].d;
sum -= roads[i].d * roads[i].p;
}
else
{
sum -= m * roads[i].p;
break;
}
}
printf("%d\n",sum);
}
return 0;
}

48
HDOJ/3091_autoAC.cpp Normal file
View File

@ -0,0 +1,48 @@
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <iomanip>
#define INF 99999999
typedef __int64 LL;
using namespace std;
const int MAX=(1<<18)+10;
int n,m;
LL dp[MAX][20];
bool edge[20][20];
void DP(){
int bit=1<<n;
memset(dp,0,sizeof dp);
dp[1][0]=1;
for(int i=1;i<bit;++i){
for(int j=0;j<n;++j){
if(dp[i][j] == 0)continue;
for(int k=1;k<n;++k){
if(i&(1<<k))continue;
if(!edge[j][k])continue;
dp[i|(1<<k)][k]+=dp[i][j];
}
}
}
LL sum=0;
for(int i=0;i<n;++i)if(edge[0][i])sum+=dp[bit-1][i];
printf("%I64d\n",sum);
}
int main(){
int u,v;
while(~scanf("%d%d",&n,&m)){
memset(edge,false,sizeof edge);
for(int i=0;i<m;++i){
scanf("%d%d",&u,&v);
--u,--v;
edge[u][v]=edge[v][u]=true;
}
DP();
}
return 0;
}

67
HDOJ/3092_autoAC.cpp Normal file
View File

@ -0,0 +1,67 @@
#include <iostream>
#include <cmath>
using namespace std;
const int maxp=44;
int p[maxp];
int pp;
void sai()
{
int i,j;
pp=0;
for (i=2;i<3010;i++)
{
for (j=2;j*j<=i;j++)
if (i%j==0) break;
if (j*j>i) p[pp++]=i;
if (pp>=maxp) break;
}
}
int res[maxp],a[maxp];
int n,m;
double ber[3010][maxp]={0};
double fres;
void dfs(int k,double mut,int s)
{
if (ber[n-s][k]>mut+0.01) return ;
else if (mut>ber[n-s][k]) ber[n-s][k]=mut;
int i=p[k];
if (k>=pp||p[k]>s)
{
if (mut>fres)
{
memcpy(res,a,sizeof(a));
fres=mut;
}
return ;
}
dfs(k+1,mut,s);
for (a[k]=1;i<=s;a[k]++)
{
mut=mut+log(p[k]);
dfs(k+1,mut,s-i);
i*=p[k];
}
a[k]=0;
}
void oot()
{
int i,j,k;
k=1;
for (i=0;i<maxp;i++)
for (j=0;j<res[i];j++)
k=k*p[i]%m;
printf("%d\n",k%m);
}
int main()
{
sai();
while (scanf("%d%d",&n,&m)!=EOF)
{
memset(ber,0,sizeof(ber));
memset(a,0,sizeof(a));
fres=-1;
dfs(0,0,n);
oot();
}
return 0;
}

46
HDOJ/3094_autoAC.cpp Normal file
View File

@ -0,0 +1,46 @@
#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
using namespace std;
const int N = 100000+10;
vector<int> g[N];
bool vis[N];
int w[N];
void dfs(int u)
{
vis[u]=true;
int size=g[u].size();
w[u]=0;
for(int i=0;i<size;i++)
{
int v=g[u][i];
if(vis[v]) continue;
dfs(v);
w[u]^=(w[v]+1);
}
}
int main()
{
int T,n;
int a,b;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=0;i<=n;i++)
g[i].clear();
for(int i=1;i<n;i++)
{
scanf("%d %d",&a,&b);
g[a].push_back(b);
g[b].push_back(a);
}
memset(vis,false,sizeof(vis));
dfs(1);
if(w[1])
puts("Alice");
else puts("Bob");
}
return 0;
}

121
HDOJ/3096_autoAC.cpp Normal file
View File

@ -0,0 +1,121 @@
#include<stdio.h>
#include<string.h>
int dir[18][2] =
{
{ -1, -1,}, { -1, 0}, {0, -1}, {0, 1}, {1, 0}, {1, 1},
{ -1, -1,}, { -1, 0}, {0, -1}, {0, 1}, {1, -1}, {1, 0},
{ -1, 0,}, { -1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}
};
__int64 mlen[21];
__int64 msle[21];
__int64 bl, m, l, sn;
__int64 n;
__int64 map[101];
__int64 res[101];
__int64 ret[101][101];
__int64 Mat[101][101];
__int64 temp[101][101];
void make_mlen()
{
int i, j;
sn = bl * 2 - 1;
for( i = 0 ; i < bl; i++)
mlen[i] = i + bl;
for( j = bl - 2; j >= 0 ; j--)
mlen[i++] = mlen[j];
msle[0] = 0;
for( i = 1; i <= sn ; i++)
msle[i] = msle[i - 1] + mlen[i - 1];
n = msle[sn];
}
void input()
{
int i;
for( i = 0 ; i < n; i++)
scanf("%I64d", &map[i]);
}
void make_matrix()
{
int i, j, k, ii, jj, kk;
memset(Mat, 0, sizeof(Mat));
for( i = 0 ; i < sn; i++)
{
for(j = 0 ; j < mlen[i] ; j++)
{
Mat[msle[i] + j][msle[i] + j] = 1;
for( k = 0 ; k < 6 ; k++)
{
if( i < bl - 1 )
kk = 0;
else if( i == bl - 1)
kk = 6;
else
kk = 12;
ii = i + dir[kk + k][0];
jj = j + dir[kk + k][1];
if( ii < 0 || ii >= sn || jj < 0 || jj >= mlen[ii])
continue;
Mat[msle[i] + j][msle[ii] + jj] = 1;
}
}
}
}
void matmul(__int64 a[][101], __int64 b[][101])
{
int i, k, j;
memset(temp, 0, sizeof(temp));
for(i = 0; i < n; i++)
for(k = 0; k < n; k++)
if(a[i][k])
for(j = 0; j < n; j++)
if( b[k][j] )
temp[i][j] = (temp[i][j] + a[i][k] * b[k][j]) % m;
memcpy(a, temp, sizeof(temp));
}
void init_()
{
int i, j;
for(i = 0; i < n; i++)
for(j = 0; j < n; j++)
ret[i][j] = (i == j);
}
void q_mod(int k)
{
init_();
for(; k ; k >>= 1)
{
if(k & 1)
matmul(ret, Mat);
matmul(Mat, Mat);
}
}
__int64 solve()
{
__int64 i, j, sum, ans = 0;
for(i = 0; i < n; i++)
{
sum = 0;
for( j = 0; j < n; j++)
{
sum = (sum + map[j] * ret[i][j]) % m;
}
res[i] = sum;
}
for( i = 0 ; i < n ; i++)
ans += res[i];
return ans;
}
int main()
{
int i, j, k = 0;
while(scanf("%I64d%I64d%I64d", &bl, &m, &l), bl + m + l)
{
k++;
make_mlen();
make_matrix();
input();
q_mod(l);
printf("Case %d: %I64d\n", k, solve());
}
return 0;
}

55
HDOJ/3097_autoAC.cpp Normal file
View File

@ -0,0 +1,55 @@
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=1005;
int pre[N],count[N];
void build(int n)
{
int i;
for(i=1;i<=n;i++) {pre[i]=i;count[i]=0;}
}
int find(int k)
{
if(pre[k]==k) return k;
pre[k]=find(pre[k]);
return pre[k];
}
void get_map(int m)
{
int a,b,f1,f2;
while(m--)
{
scanf("%d%d",&a,&b);
f1=find(a);
f2=find(b);
if(f1==f2) count[f1]++;
else
{
pre[f2]=f1;
count[f1]+=count[f2]+1;
}
}
}
int Judge(int n)
{
int flag=0;
for(int i=1;i<=n;i++)
{
if(pre[i]!=i) continue;
if(count[i]%2) {flag=1;break;}
}
return flag;
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m),n||m)
{
build(n);
get_map(m);
if(Judge(n)) cout<<"No"<<endl;
else cout<<"Yes"<<endl;
}
return 0;
}