蓝桥杯打完了,打的确实lj,好好备天梯赛
备赛分为总体两部分 1.基础强化训练 2. 真题模拟
dfs
#include
using namespace std;
const int N=1010;
char g[N][N];
int dx[8]={-1,-1,-1,0,0,1,1,1};
int dy[8]={-1,0,1,-1,1,-1,0,1};
int n,m;
int cnt;
void dfs(int x,int y)
{
g[x][y]='.';
for(int i=0;i<8;i++)
{
int a=dx[i]+x,b=dy[i]+y;
if(a<0||a>=n||b<0||b>=m)continue;
if(g[a][b]=='.')continue;
dfs(a,b);
}
}
int main()
{
cin>>n>>m;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cin>>g[i][j];
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(g[i][j]=='W')
{
dfs(i,j);
cnt++;
}
}
}
cout<<cnt<<endl;
return 0;
}
bfs
#include
using namespace std;
const int N=1010;
typedef pair<int,int>PII;
char g[N][N];
int dx[8]={-1,-1,-1,0,0,1,1,1};
int dy[8]={-1,0,1,-1,1,-1,0,1};
int n,m;
int cnt;
void bfs(int x,int y)
{
queue<PII>q;
q.push({x,y});
g[x][y]='.';
while(q.size())
{
auto t=q.front();
q.pop();
int x=t.first,y=t.second;
for(int i=0;i<8;i++)
{
int a=x+dx[i],b=y+dy[i];
if(a<0||a>=n||b<0||b>=m)continue;
if(g[a][b]=='.')continue;
q.push({a,b});
g[a][b]='.';
}
}
}
int main()
{
cin>>n>>m;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cin>>g[i][j];
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(g[i][j]=='W')
{
bfs(i,j);
cnt++;
}
}
}
cout<<cnt<<endl;
return 0;
}
#include
#define x first
#define y second
using namespace std;
typedef pair<int,int>PII;
const int N=55;
int n,m;
int g[N][N];
bool st[N][N];
int dx[4]={0,-1,0,1};
int dy[4]={-1,0,1,0};
int area,cnt;
int bfs(int x,int y)
{
queue<PII>q;
q.push({x,y});
st[x][y]=true;
int s=0;
while(q.size())
{
auto t=q.front();
q.pop();
s++;
for(int i=0;i<4;i++)
{
int a=dx[i]+t.x,b=dy[i]+t.y;
if(a<0||a>=n||b<0||b>=m)continue;
if(st[a][b])continue;
if(g[t.x][t.y]>>i&1)continue;
q.push({a,b});
st[a][b]=true;
}
}
return s;
}
int main()
{
cin>>n>>m;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cin>>g[i][j];
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(!st[i][j])
{
cnt++;
area=max(area,bfs(i,j));
}
}
}
cout<<cnt<<endl;
cout<<area<<endl;
return 0;
}
#include
#define x first
#define y second
using namespace std;
typedef pair<int,int>PII;
const int N=1010;
int dx[8]={-1,-1,-1,0,0,1,1,1};
int dy[8]={-1,0,1,1,-1,1,0,-1};
int g[N][N];
bool st[N][N];
int n;
int peak,valley;
void bfs(int x,int y,bool &has_higher,bool &has_lower)
{
queue<PII>q;
q.push({x,y});
st[x][y]=true;
while(q.size())
{
auto t=q.front();
q.pop();
for(int i=0;i<8;i++)
{
int a=dx[i]+t.x,b=dy[i]+t.y;
if(a<0||a>=n||b<0||b>=n)continue;
if(g[a][b]>g[t.x][t.y])has_higher=true;
else if(g[a][b]<g[t.x][t.y])has_lower=true;
else if(!st[a][b])
{
q.push({a,b});
st[a][b]=true;
}
}
}
}
int main()
{
cin>>n;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cin>>g[i][j];
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(!st[i][j])
{
bool has_lower=false,has_higher=false;
bfs(i,j,has_higher,has_lower);
if(!has_lower)valley++;
if(!has_higher)peak++;
}
}
}
cout<<peak<<' '<<valley<<endl;
return 0;
}
#include
#define x first
#define y second
using namespace std;
typedef pair<int,int>PII;
const int N=1010;
int dx[4]={0,-1,0,1};
int dy[4]={-1,0,1,0};
int g[N][N];
bool st[N][N];
PII pre[N][N];
int n;
void bfs()
{
queue<PII>q;
q.push({0,0});
st[0][0]=true;
while(q.size())
{
auto t=q.front();
q.pop();
for(int i=0;i<4;i++)
{
int a=dx[i]+t.x,b=dy[i]+t.y;
if(a<0||a>=n||b<0||b>=n)continue;
if(st[a][b])continue;
if(g[a][b]==1)continue;
st[a][b]=true;
q.push({a,b});
pre[a][b]=t;
}
}
PII u={n-1,n-1};
vector<PII>v;
while(u.x!=0||u.y!=0)
{
v.push_back(u);
auto t=pre[u.x][u.y];
u=t;
}
cout<<0<<' '<<0<<endl;
for(int i=v.size()-1;i>=0;i--)cout<<v[i].x<<' '<<v[i].y<<endl;
}
int main()
{
cin>>n;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
cin>>g[i][j];
bfs();
}
#include
using namespace std;
const int N=1e5+10;
int dist[N];
bool st[N];
int n,k;
void bfs()
{
memset(dist,0x3f,sizeof dist);
queue<int>q;
q.push(n);
dist[n]=0;
st[n]=true;
while(q.size())
{
auto t=q.front();
q.pop();
if(t==k)break;
int a=t-1,b=t+1,c=2*t;
vector<int>v;
v.push_back(a),v.push_back(b),v.push_back(c);
for(int i=0;i<v.size();i++)
{
int x=v[i];
if(x<0)continue;
if(x>=N)continue;
if(st[x])continue;
st[x]=true;
dist[x]=dist[t]+1;
q.push(x);
}
}
cout<<dist[k]<<endl;
}
int main()
{
cin>>n>>k;
bfs();
}
#include
#define x first
#define y second
using namespace std;
const int N=1010;
int dx[4]={-1,0,1,0};
int dy[4]={0,1,0,-1};
char g[N][N];
int n,m;
typedef pair<int,int>PII;
queue<PII>q;
int dist[N][N];
void bfs()
{
memset(dist,0x3f,sizeof dist);
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(g[i][j]=='1')
{
q.push({i,j});
dist[i][j]=0;
}
}
}
while(q.size())
{
auto t=q.front();
q.pop();
for(int i=0;i<4;i++)
{
int a=dx[i]+t.x,b=dy[i]+t.y;
if(a<0||a>n||b<0||b>m)continue;
if(dist[a][b]!=0x3f3f3f3f)continue;
dist[a][b]=dist[t.x][t.y]+1;
q.push({a,b});
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cout<<dist[i][j]<<' ';
}
cout<<endl;
}
}
int main()
{
cin>>n>>m;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
cin>>g[i][j];
bfs();
}
#include
using namespace std;
char g[2][4];
unordered_map<string,pair<char,string>>pre;
unordered_map<string,int>dist;
void _set(string state)
{
for(int i=0;i<4;i++)g[0][i]=state[i];
for(int i=7,j=0;j<4;i--,j++)g[1][j]=state[i];
}
string get()
{
string res;
for(int i=0;i<4;i++)res+=g[0][i];
for(int i=3;i>=0;i--)res+=g[1][i];
return res;
}
string move0(string state)
{
_set(state);
for(int i=0;i<4;i++)swap(g[0][i],g[1][i]);
return get();
}
string move1(string state)
{
_set(state);
char v0=g[0][3],v1=g[1][3];
for(int i=3;i>=0;i--)
{
g[0][i]=g[0][i-1];
g[1][i]=g[1][i-1];
}
g[0][0]=v0,g[1][0]=v1;
return get();
}
string move2(string state)
{
_set(state);
int v=g[0][1];
g[0][1]=g[1][1];
g[1][1]=g[1][2];
g[1][2]=g[0][2];
g[0][2]=v;
return get();
}
int bfs(string start, string ed)
{
if(start==ed)return 0;
queue<string>q;
q.push(start);
dist[start]=0;
while(q.size())
{
auto t=q.front();
q.pop();
string m[3];
m[0]=move0(t);
m[1]=move1(t);
m[2]=move2(t);
for(int i=0;i<3;i++)
{
if(!dist.count(m[i]))
{
dist[m[i]]=dist[t]+1;
pre[m[i]]={'A'+i,t};
if(m[i]==ed)return dist[ed];
q.push(m[i]);
}
}
}
return -1;
}
int main()
{
int x;
string start,ed;
for(int i=0;i<8;i++)
{
cin>>x;
ed+=char(x+'0');
}
for(int i=1;i<=8;i++)
start+=char(i+'0');
int step=bfs(start,ed);
cout<<step<<endl;
string res;
while(ed!=start)
{
res+=pre[ed].first;
ed=pre[ed].second;
}
reverse(res.begin(),res.end());
if(step>0)cout<<res<<endl;
return 0;
}
#include
#define x first
#define y second
using namespace std;
typedef pair<int,int>PII;
const int N=510,M=N*N;
int n,m;
char g[N][N];
int dist[N][N];
bool st[N][N];
char cs[]="\\/\\/";
int dx[4]={-1,-1,1,1},dy[4]={-1,1,1,-1};
int ix[4]={-1,-1,0,0},iy[4]={-1,0,0,-1};
int bfs()
{
memset(dist,0x3f,sizeof dist);
memset(st,false,sizeof st);
dist[0][0]=0;
deque<PII>q;
q.push_back({0,0});
while(q.size())
{
auto t=q.front();
q.pop_front();
if(st[t.x][t.y])continue;
st[t.x][t.y]=true;
for(int i=0;i<4;i++)
{
int a=dx[i]+t.x,b=dy[i]+t.y;
if(a<0||a>n||b<0||b>m)continue;
int ca=t.x+ix[i],cb=t.y+iy[i];
int d=dist[t.x][t.y]+(g[ca][cb]!=cs[i]);
if(d<dist[a][b])
{
dist[a][b]=d;
if(g[ca][cb]!=cs[i])q.push_back({a,b});
else q.push_front({a,b});
}
}
}
return dist[n][m];
}
int main()
{
int t;
cin>>t;
while(t--)
{
cin>>n>>m;
for(int i=0;i<n;i++)cin>>g[i];
int t=bfs();
if(t==0x3f3f3f3f)puts("NO SOLUTION");
else cout<<t<<endl;
}
return 0;
}
#include
using namespace std;
const int N=110;
char g[N][N];
bool st[N][N];
int dx[4]={0,-1,0,1};
int dy[4]={-1,0,1,0};
int ax,ay,bx,by;
int n;
int t;
void dfs(int x,int y)
{
st[x][y]=true;
for(int i=0;i<4;i++)
{
int a=x+dx[i],b=y+dy[i];
if(a<0||a>=n||b<0||b>=n)continue;
if(g[a][b]=='#')continue;
if(st[a][b])continue;
dfs(a,b);
}
}
void solve()
{
memset(st,false,sizeof st);
cin>>n;
for(int i=0;i<n;i++)cin>>g[i];
cin>>ax>>ay>>bx>>by;
dfs(ax,ay);
if(st[bx][by]&&g[ax][ay]!='#')puts("YES");
else puts("NO");
}
int main()
{
cin>>t;
while(t--)solve();
return 0;
}
dfs
#include
#define x first
#define y second
using namespace std;
typedef pair<int,int>PII;
int m,n;
const int N=31;
int dx[4]={0,-1,0,1};
int dy[4]={-1,0,1,0};
char g[N][N];
bool st[N][N];
PII start;
int res;
void dfs(PII t)
{
res++;
st[t.x][t.y]=true;
for(int i=0;i<4;i++)
{
int a=dx[i]+t.x,b=dy[i]+t.y;
if(a<0||a>=n||b<0||b>=m)continue;
if(st[a][b])continue;
if(g[a][b]=='#')continue;
auto x=make_pair(a,b);
dfs(x);
}
}
int main()
{
while(cin>>m>>n,m||n)
{
memset(st,false,sizeof st);
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cin>>g[i][j];
if(g[i][j]=='@')start={i,j};
}
}
res=0;
dfs(start);
cout<<res<<endl;
}
}
#include
using namespace std;
const int N=15;
int dx[8]={2,1,-1,-2,-2,-1,1,2};//方向数组
int dy[8]={1,2,2,1,-1,-2,-2,-1};
int g[N][N];
bool st[N][N];
int n,m;
int res;
int t;
void dfs(int x,int y,int depth)
{
if(depth==n*m)
{
res++;
return;
}
st[x][y]=true;
for(int i=0;i<8;i++)
{
int a=dx[i]+x,b=dy[i]+y;
if(a<0||a>=n||b<0||b>=m)continue;
if(st[a][b])continue;
dfs(a,b,depth+1);
}
st[x][y]=false;
}
void solve()
{
cin>>n>>m;
int x,y;
cin>>x>>y;
res=0;
memset(st,false,sizeof st);
dfs(x,y,1);
cout<<res<<endl;
}
int main()
{
cin>>t;
while(t--)solve();
return 0;
}
#include
using namespace std;
const int N = 21;
int n;
string word[N];
int g[N][N];
int used[N];
int res;
char start;
void dfs(string dragon, int last)
{
res = max(res, (int)dragon.size());
used[last]++;
for (int i = 0; i < n; i++)
{
if (g[last][i] && used[i] < 2)
{
dfs(dragon + word[i].substr(g[last][i]), i);
}
}
used[last]--;
}
int main()
{
cin >> n;
for (int i = 0; i < n; i++)cin >> word[i];
cin >> start;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
string a = word[i], b = word[j];
for (int k = 1; k < min(a.size(), b.size()); k++)
{
if (a.substr(a.size() - k, k) == b.substr(0, k))
{
g[i][j] = k;
break;
}
}
}
}
for (int i = 0; i < n; i++)
{
if (start == word[i][0])
dfs(word[i], i);
}
cout << res << endl;
return 0;
}
#include
#define x first
#define y second
using namespace std;
typedef pair<int,int>PII;
const int N=2510;
const int M=6200*2+10;
int h[N],e[M],ne[M],w[M],idx;
int n,m;
int start,ed;
int dist[N];
bool st[N];
void add(int a,int b,int c)
{
e[idx]=b,ne[idx]=h[a],w[idx]=c,h[a]=idx++;
}
void dijkstra()
{
memset(dist,0x3f,sizeof dist);
dist[start]=0;
priority_queue<PII,vector<PII>,greater<PII>>heap;
heap.push({dist[start],start});
while(heap.size())
{
auto t=heap.top();
heap.pop();
if(st[t.y])continue;
st[t.y]=true;
for(int i=h[t.y];~i;i=ne[i])
{
int j=e[i];
if(dist[j]>dist[t.y]+w[i])
{
dist[j]=dist[t.y]+w[i];
heap.push({dist[j],j});
}
}
}
cout<<dist[ed]<<endl;
}
int main()
{
cin>>n>>m>>start>>ed;
memset(h,-1,sizeof h);
while(m--)
{
int a,b,c;
cin>>a>>b>>c;
add(a,b,c);
add(b,a,c);
}
dijkstra();
return 0;
}
#include
using namespace std;
const int N=110;
const int M=410;
int g[N][N];
int n,m;
int dist[N];
bool st[N];
void dijkstra()
{
memset(dist,0x3f,sizeof dist);
dist[1]=0;
for(int i=0;i<n;i++)
{
int t=-1;
for(int j=1;j<=n;j++)
{
if(!st[j]&&(t==-1||dist[j]<dist[t]))
{
t=j;
}
}
st[t]=true;
for(int j=1;j<=n;j++)
dist[j]=min(dist[j],dist[t]+g[t][j]);
}
int res=0;
for(int i=1;i<=n;i++)
{
res=max(res,dist[i]);
}
if(res!=0x3f3f3f3f)
cout<<res<<endl;
else cout<<-1<<endl;
}
int main()
{
cin>>n>>m;
memset(g,0x3f,sizeof g);
while(m--)
{
int a,b,c;
cin>>a>>b>>c;
g[a][b]=g[b][a]=c;
}
dijkstra();
return 0;
}
#include
using namespace std;
const int N=1010;
const int M=3010;
int h[N],e[M],ne[M],w[M],idx;
int dist[N];
int id[N];
bool st[N];
int n,m,p;
void add(int a,int b,int c)
{
e[idx]=b,ne[idx]=h[a],w[idx]=c,h[a]=idx++;
}
int spfa(int start)
{
memset(dist,0x3f,sizeof dist);
memset(st,false,sizeof st);
queue<int>q;
q.push(start);
st[start]=true;
dist[start]=0;
while(q.size())
{
auto t=q.front();
q.pop();
st[t]=false;
for(int i=h[t];~i;i=ne[i])
{
int j=e[i];
if(dist[j]>dist[t]+w[i])
{
dist[j]=dist[t]+w[i];
if(!st[j])
{
st[j]=true;
q.push(j);
}
}
}
}
int res=0;
for(int i=1;i<=n;i++)
{
res+=dist[id[i]];
if(dist[i]==0x3f3f3f3f)return 0x3f3f3f3f;
}
return res;
}
int main()
{
cin>>n>>p>>m;
memset(h,-1,sizeof h);
for(int i=1;i<=n;i++)cin>>id[i];
while(m--)
{
int a,b,c;
cin>>a>>b>>c;
add(a,b,c);
add(b,a,c);
}
int res=0x3f3f3f3f;
for(int i=1;i<=p;i++)res=min(res,spfa(i));
cout<<res<<endl;
return 0;
}
#include
using namespace std;
const int N=2010;
double g[N][N];
double dist[N];
bool st[N];
int n,m;
int start,ed;
void dijkstra()
{
dist[start]=1;
for(int i=0;i<n;i++)
{
int t=-1;
for(int j=1;j<=n;j++)
{
if(!st[j]&&(t==-1||dist[t]<dist[j]))
{
t=j;
}
}
st[t]=true;
for(int j=1;j<=n;j++)
{
dist[j]=max(dist[j],dist[t]*g[t][j]);
}
}
printf("%.8f",100/dist[ed]);
}
int main()
{
cin>>n>>m;
while(m--)
{
int a,b,c;
cin>>a>>b>>c;
double z=(100-c)*1.0/100;
g[a][b]=g[b][a]=max(g[a][b],z);
}
cin>>start>>ed;
dijkstra();
return 0;
}
#include
using namespace std;
const int N=510;
bool g[N][N];
int n,m;
int dist[N];
bool st[N];
int stop[N];
void bfs()
{
memset(dist,0x3f,sizeof dist);
queue<int>q;
q.push(1);
st[1]=true;
dist[1]=0;
while(q.size())
{
auto t=q.front();
q.pop();
for(int i=1;i<=n;i++)
{
if(g[t][i])
{
if(st[i])continue;
st[i]=true;
q.push(i);
dist[i]=dist[t]+1;
}
}
}
if(dist[n]==0x3f3f3f3f)puts("NO");
else cout<<max(dist[n]-1,0)<<endl;
}
int main()
{
cin>>m>>n;
string line;
getchar();
while(m--)
{
getline(cin,line);
stringstream ssin(line);
int cnt=0,p;
while(ssin>>p)stop[cnt++]=p;
for(int i=0;i<cnt;i++)
{
for(int j=i+1;j<cnt;j++)
{
g[stop[i]][stop[j]]=true;
}
}
}
bfs();
return 0;
}
#include
using namespace std;
const int N=110;
int w[N][N];
int level[N];
bool st[N];
int dist[N];
int m,n;
int dijkstra(int down,int up)
{
memset(dist,0x3f,sizeof dist);
memset(st,false,sizeof st);
dist[0]=0;
for(int i=1;i<=n+1;i++)
{
int t=-1;
for(int j=0;j<=n;j++)
{
if(!st[j]&&(t==-1||dist[j]<dist[t]))
t=j;
}
st[t]=true;
for(int j=0;j<=n;j++)
{
if(level[j]>=down&&level[j]<=up)
{
dist[j]=min(dist[j],dist[t]+w[t][j]);
}
}
}
return dist[1];
}
int main()
{
cin>>m>>n;
memset(w,0x3f,sizeof w);
for(int i=1;i<=n;i++)
{
int price,cnt;
cin>>price>>level[i]>>cnt;
w[0][i]=min(price,w[0][i]);
while(cnt--)
{
int id,cost;
cin>>id>>cost;
w[id][i]=min(w[id][i],cost);
}
}
int res=0x3f3f3f3f;
for(int i=level[1]-m;i<=level[1];i++)
{
res=min(res,dijkstra(i,i+m));
}
cout<<res<<endl;
return 0;
}
#include
using namespace std;
const int N=15;
int n;
int a[N];
vector<int>g[N];
int cnt,res=N;
int gcd(int a,int b)
{
return b?gcd(b,a%b):a;
}
bool check(int u,int c)
{
for(int i=0;i<g[c].size();i++)
{
if(gcd(u,g[c][i])>1)return false;
}
return true;
}
void dfs(int u)
{
if(u==n)
{
res=min(res,cnt);
return;
}
for(int i=0;i<cnt;i++)
{
if(check(a[u],i))
{
g[i].push_back(a[u]);
dfs(u+1);
g[i].pop_back();
}
}
g[cnt++].push_back(a[u]);
dfs(u+1);
g[--cnt].pop_back();
}
int main()
{
cin>>n;
for(int i=0;i<n;i++)cin>>a[i];
dfs(0);
cout<<res<<endl;
return 0;
}
#include
using namespace std;
const int N=20;
int n,m;
int w[N];
int sum[N];
int ans=N;
void dfs(int u,int k)
{
if(k>=ans)return;
if(u==n)
{
ans=k;
return;
}
for(int i=0;i<k;i++)
{
if(sum[i]+w[u]<=m)
{
sum[i]+=w[u];
dfs(u+1,k);
sum[i]-=w[u];
}
}
sum[k]=w[u];
dfs(u+1,k+1);
sum[k]=0;
}
int main()
{
cin>>n>>m;
for(int i=0;i<n;i++)cin>>w[i];
sort(w,w+n,greater<int>());
dfs(0,0);
cout<<ans<<endl;
return 0;
}
#include
#define x first
#define y second
using namespace std;
typedef pair<int,int>PII;
const int N=50010;
const int M=2e5+10;
int h[N],e[M],ne[M],w[M],idx;
int n,m;
bool st[N];
int dist[6][N];
int source[6];
void add(int a,int b,int c)
{
e[idx]=b,w[idx]=c,ne[idx]=h[a],h[a]=idx++;
}
int dfs(int u,int start,int distance)
{
if(u==6)return distance;
int res=0x3f3f3f3f;
st[start]=true;
for(int i=1;i<=5;i++)
{
int next=source[i];
if(!st[i]&&dist[start][next]!=0x3f3f3f3f)
{
res=min(res,dfs(u+1,i,distance+dist[start][next]));
}
}
st[start]=false;
return res;
}
void dijkstra(int start,int dist[])
{
memset(dist,0x3f,4*N);
memset(st,false, sizeof st);
priority_queue<PII,vector<PII>,greater<PII>>heap;
dist[start]=0;
heap.push({dist[start],start});
while(heap.size())
{
auto t=heap.top();
heap.pop();
if(st[t.y])continue;
st[t.y]=true;
for(int i=h[t.y];~i;i=ne[i])
{
int j=e[i];
if(dist[j]>dist[t.y]+w[i])
{
dist[j]=dist[t.y]+w[i];
heap.push({dist[j],j});
}
}
}
}
int main()
{
memset(h,-1,sizeof h);
cin>>n>>m;
for(int i=1;i<=5;i++)cin>>source[i];
source[0]=1;
while(m--)
{
int a,b,c;
cin>>a>>b>>c;
add(a,b,c);
add(b,a,c);
}
for(int i=0;i<6;i++)dijkstra(source[i],dist[i]);
memset(st,false,sizeof st);
cout<<dfs(1,0,0)<<endl;
return 0;
}
#include
using namespace std;
const int N=1010;
const int M=20010;
int h[N],e[M],ne[M],w[M],idx;
bool st[N];
int dist[N];
int n,m,k;
void add(int a,int b,int c)
{
e[idx]=b,ne[idx]=h[a],w[idx]=c,h[a]=idx++;
}
bool check(int bound)
{
memset(dist,0x3f,sizeof dist);
memset(st,false ,sizeof st);
dist[1]=0;
deque<int>q;
q.push_back(1);
while(q.size())
{
auto t=q.front();
q.pop_front();
if(st[t])continue;
st[t]=true;
for(int i=h[t];~i;i=ne[i])
{
int j=e[i];
int x=w[i]>bound;
if(dist[j]>dist[t]+x)
{
dist[j]=dist[t]+x;
if(x)q.push_back(j);
else q.push_front(j);
}
}
}
return dist[n]<=k;
}
int main()
{
cin>>n>>m>>k;
memset(h,-1,sizeof h);
while(m--)
{
int a,b,c;
cin>>a>>b>>c;
add(a,b,c);
add(b,a,c);
}
int l=0,r=1e6+10;
while(l<r)
{
int mid=l+r>>1;
if(check(mid))r=mid;
else l=mid+1;
}
if(r==1e6+10)cout<<-1<<endl;
else cout<<r<<endl;
return 0;
}
#include
using namespace std;
const int N=100010,M=2000010;
int n,m;
int price[N];
int h[N],rh[N],e[M],ne[M],idx;
int dmin[N],dmax[N];
bool st[N];
void add(int *h,int a,int b)
{
e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
void spfa(int *dist,int start,int *h,bool f)
{
queue<int>q;
memset(st,false,sizeof st);
if(f)memset(dist,0x3f,4*N);
q.push(start);
st[start]=true;
dist[start]=price[start];
while(q.size())
{
auto t=q.front();
q.pop();
st[t]=false;
for(int i=h[t];~i;i=ne[i])
{
int j=e[i];
if(f&&dist[j]>min(dist[t],price[j])||!f&&dist[j]<max(dist[t],price[j]))
{
if(f)dist[j]=min(dist[t],price[j]);
else dist[j]=max(dist[t],price[j]);
if(st[j])continue;
st[j]=true;
q.push(j);
}
}
}
}
int main()
{
cin>>n>>m;
memset(h,-1,sizeof h);
memset(rh,-1,sizeof rh);
for(int i=1;i<=n;i++)cin>>price[i];
while(m--)
{
int a,b,c;
cin>>a>>b>>c;
add(h,a,b);
add(rh,b,a);
if(c==2)add(h,b,a),add(rh,a,b);
}
spfa(dmin,1,h,true);
spfa(dmax,n,rh,false);
int res=0;
for(int i=1;i<=n;i++)res=max(res,dmax[i]-dmin[i]);
cout<<res<<endl;
return 0;
}
#include
using namespace std;
const int N=1010;
const int M=40010;
int h[N],e[M],ne[M],w[M],idx;
int n,m,s;
bool st[N];
int dist[N];
void add(int a,int b,int c)
{
e[idx]=b,ne[idx]=h[a],w[idx]=c,h[a]=idx++;
}
int spfa()
{
memset(st,false,sizeof st);
memset(dist,0x3f,sizeof dist);
queue<int>q;
dist[0]=0;
q.push(0);
st[0]=true;
while(q.size())
{
auto t=q.front();
q.pop();
st[t]=false;
for(int i=h[t];~i;i=ne[i])
{
int j=e[i];
if(dist[j]>dist[t]+w[i])
{
dist[j]=dist[t]+w[i];
if(st[j])continue;
st[j]=true;
q.push(j);
}
}
}
return dist[s];
}
int main()
{
while(cin>>n>>m>>s)
{
memset(h,-1,sizeof h);
idx=0;
while(m--)
{
int a,b,c;
cin>>a>>b>>c;
add(a,b,c);
}
int k;
cin>>k;
for(int i=0;i<k;i++)
{
int x;
cin>>x;
add(0,x,0);
}
if(spfa()!=0x3f3f3f3f)cout<<dist[s]<<endl;
else puts("-1");
}
return 0;
}
#include
using namespace std;
const int N=1e5+10;
const int M=4e5+10;
const int mod=100003;
int h[N],e[M],ne[M],idx;
int cnt[N];
int dist[N];
int n,m;
bool st[N];
void add(int a,int b)
{
e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
void bfs()
{
memset(dist,0x3f,sizeof dist);
cnt[1]=1;
queue<int>q;
q.push(1);
dist[1]=0;
while(q.size())
{
auto t=q.front();
q.pop();
if(st[t])continue;
st[t]=true;
for(int i=h[t];~i;i=ne[i])
{
int j=e[i];
if(dist[j]>dist[t]+1)
{
dist[j]=dist[t]+1;
cnt[j]=cnt[t]%mod;
q.push(j);
}
else if(dist[j]==dist[t]+1)
{
cnt[j]=(cnt[t]+cnt[j])%mod;
}
}
}
for(int i=1;i<=n;i++)cout<<cnt[i]<<endl;
}
int main()
{
cin>>n>>m;
memset(h,-1,sizeof h);
while(m--)
{
int a,b;
cin>>a>>b;
add(a,b);
add(b,a);
}
bfs();
return 0;
}
#include
#define x first
#define y second
using namespace std;
const int N=25010,M=150010;
typedef pair<int,int>PII;
int h[N],e[M],ne[M],w[M],idx;
int dist[N];
bool st[N];
int n,mr,mp,s;
int id[N],bcnt;
int deg[N];
queue<int>q;
vector<int>block[N];
void add(int a,int b,int c)
{
e[idx]=b,w[idx]=c,ne[idx]=h[a],h[a]=idx++;
}
void dfs(int u)
{
block[bcnt].push_back(u);
id[u]=bcnt;
for(int i=h[u];~i;i=ne[i])
{
int j=e[i];
if(!id[j])dfs(j);
}
}
void dijkstra(int block_id)
{
priority_queue<PII,vector<PII>,greater<PII>>heap;
for(auto u:block[block_id])heap.push({dist[u],u});
while(heap.size())
{
auto t=heap.top();
heap.pop();
if(st[t.y])continue;
st[t.y]=true;
for(int i=h[t.y];~i;i=ne[i])
{
int j=e[i];
if(dist[j]>dist[t.y]+w[i])
{
dist[j]=dist[t.y]+w[i];
if(id[j]==block_id)heap.push({dist[j],j});
}
if(id[j]!=block_id&&--deg[id[j]]==0)q.push(id[j]);
}
}
}
void topsort()
{
memset(dist,0x3f,sizeof dist);
dist[s]=0;
for(int i=1;i<=bcnt;i++)
{
if(!deg[i])q.push(i);
}
while(q.size())
{
auto t=q.front();
q.pop();
dijkstra(t);
}
}
int main()
{
cin>>n>>mr>>mp>>s;
memset(h,-1,sizeof h);
for(int i=0;i<mr;i++)
{
int a,b,c;
cin>>a>>b>>c;
add(a,b,c);
add(b,a,c);
}
for(int i=1;i<=n;i++)
{
if(!id[i])
{
bcnt++;
dfs(i);
}
}
for(int i=0;i<mp;i++)
{
int a,b,c;
cin>>a>>b>>c;
add(a,b,c);
deg[id[b]]++;
}
topsort();
for(int i=1;i<=n;i++)
{
if(dist[i]>0x3f3f3f3f/2)puts("NO PATH");
else cout<<dist[i]<<endl;
}
return 0;
}
#include
using namespace std;
const int N=1010,M=10010;
int h[N],e[M],ne[M],w[M],idx;
int cnt[N][2];
int dist[N][2];
bool st[N][2];
int n,m,s,t;
struct node
{
int id,type,distance;
bool operator>(const node&a)const
{
return distance>a.distance;
}
};
void add(int a,int b,int c)
{
e[idx]=b,ne[idx]=h[a],w[idx]=c,h[a]=idx++;
}
int dijkstra()
{
memset(st,false,sizeof st);
memset(dist,0x3f,sizeof dist);
memset(cnt,0,sizeof cnt);
priority_queue<node,vector<node>,greater<node>>heap;
heap.push({s,0,0});
dist[s][0]=0;
cnt[s][0]=1;
while(heap.size())
{
auto t=heap.top();
heap.pop();
int ver=t.id,type=t.type,distance=t.distance;
if(st[ver][type])continue;
st[ver][type]=true;
for(int i=h[ver];~i;i=ne[i])
{
int j=e[i];
if(dist[j][0]>dist[ver][type]+w[i])
{
dist[j][1]=dist[j][0];
cnt[j][1]=cnt[j][0];
heap.push({j,1,dist[j][1]});
dist[j][0]=dist[ver][type]+w[i];
cnt[j][0]=cnt[ver][type];
heap.push({j,0,dist[j][0]});
}
else if(dist[j][0]==dist[ver][type]+w[i])
{
cnt[j][0]+=cnt[ver][type];
}
else if(dist[j][1]>dist[ver][type]+w[i])
{
dist[j][1]=dist[ver][type]+w[i];
cnt[j][1]=cnt[ver][type];
heap.push({j,1,dist[j][1]});
}
else if(dist[j][1]==dist[ver][type]+w[i])
{
cnt[j][1]+=cnt[ver][type];
}
}
}
int res=cnt[t][0];
if(dist[t][0]+1==dist[t][1])res+=cnt[t][1];
return res;
}
int main()
{
int _;
cin>>_;
while(_--)
{
memset(h,-1,sizeof h);
cin>>n>>m;
for(int i=0;i<m;i++)
{
int a,b,c;
cin>>a>>b>>c;
add(a,b,c);
}
cin>>s>>t;
cout<<dijkstra()<<endl;
}
return 0;
}
#include
#define x first
#define y second
using namespace std;
typedef pair<double,double>PDD;
const int N=155;
const double INF=1e20;
int n;
PDD q[N];
double d[N][N];
double maxd[N];
char g[N][N];
double get_dist(PDD a, PDD b)
{
double dx=a.x-b.x;
double dy=a.y-b.y;
return sqrt(dx*dx+dy*dy);
}
int main()
{
cin>>n;
for(int i=0;i<n;i++)cin>>q[i].x>>q[i].y;
for(int i=0;i<n;i++)cin>>g[i];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(i==j)d[i][j]=0;
else if(g[i][j]=='1')d[i][j]=get_dist(q[i],q[j]);
else d[i][j]=INF;
}
}
for(int k=0;k<n;k++)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
}
}
}
double r1=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(d[i][j]<INF/2)maxd[i]=max(maxd[i],d[i][j]);
}
r1=max(r1,maxd[i]);
}
double r2=INF;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(d[i][j]>INF/2)r2=min(r2,maxd[i]+maxd[j]+get_dist(q[i],q[j]));
}
}
printf("%.6f",max(r1,r2));
return 0;
}
#include
using namespace std;
const int N=110;
int n;
int w[N][N];
int dist[N];
bool st[N];
int prim()
{
memset(dist,0x3f,sizeof dist);
dist[1]=0;
int res=0;
for(int i=1;i<=n;i++)
{
int t=-1;
for(int j=1;j<=n;j++)
{
if(!st[j]&&(t==-1||dist[j]<dist[t]))
t=j;
}
st[t]=true;
res+=dist[t];
for(int j=1;j<=n;j++)
{
dist[j]=min(dist[j],w[t][j]);
}
}
return res;
}
int main()
{
cin>>n;
memset(w,0x3f,sizeof w);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
cin>>w[i][j];
}
}
cout<<prim()<<endl;
return 0;
}
#include
using namespace std;
const int N=110,M=210;
struct node
{
int a,b,w;
bool operator<(const node &t)const
{
return w<t.w;
}
}e[M];
int f[N];
int n,m;
int find(int x)
{
if(x!=f[x])f[x]=find(f[x]);
return f[x];
}
void unite(int x,int y)
{
int a=find(x);
int b=find(y);
if(a!=b)f[b]=a;
}
int main()
{
cin>>n>>m;
for(int i=0;i<N;i++)f[i]=i;
for(int i=0;i<m;i++)cin>>e[i].a>>e[i].b>>e[i].w;
sort(e,e+m);
int res=0;
for(int i=0;i<m;i++)
{
int a=e[i].a,b=e[i].b,w=e[i].w;
if(find(a)!=find(b))unite(a,b);
else res+=w;
}
cout<<res<<endl;
return 0;
}
#include
using namespace std;
const int N=310,M=8010;
struct node
{
int a,b,w;
bool operator<(const node &t)const
{
return w<t.w;
}
}e[M];
int n,m;
int f[N];
int find(int x)
{
if(x!=f[x])f[x]=find(f[x]);
return f[x];
}
void unite(int a,int b)
{
int x=find(a);
int y=find(b);
if(x!=y)f[y]=x;
}
int main()
{
cin>>n>>m;
for(int i=0;i<N;i++)f[i]=i;
for(int i=0;i<m;i++)cin>>e[i].a>>e[i].b>>e[i].w;
sort(e,e+m);
int res=0;
for(int i=0;i<m;i++)
{
int a=e[i].a,b=e[i].b,w=e[i].w;
if(find(a)!=find(b))
{
unite(a,b);
res=w;
}
}
cout<<n-1<<' '<<res<<endl;
return 0;
}
#include
using namespace std;
const int N=2010;
const int M=10010;
int n,m;
int f[N];
struct node
{
int a,b,w;
bool operator<(const node &t)const
{
return w<t.w;
}
}e[M];
int find(int x)
{
if(x!=f[x])f[x]=find(f[x]);
return f[x];
}
void unite(int a,int b)
{
int x=find(a);
int y=find(b);
if(x!=y)f[y]=x;
}
int main()
{
cin>>n>>m;
int res=0;
for(int i=0;i<N;i++)f[i]=i;
int k=0;
for(int i=0;i<m;i++)
{
int t,a,b,c;
cin>>t>>a>>b>>c;
if(t==1)
{
if(find(a)!=find(b))
unite(a,b);
res+=c;
}
else e[k++]={a,b,c};
}
sort(e,e+k);
for(int i=0;i<k;i++)
{
int a=e[i].a,b=e[i].b,w=e[i].w;
if(find(a)!=find(b))
{
unite(a,b);
res+=w;
}
}
cout<<res<<endl;
return 0;
}
#include
using namespace std;
const int N=310;
int w[N][N];
int dist[N];
bool st[N];
int n;
void prim()
{
memset(dist,0x3f,sizeof dist);
dist[1]=0;
int res=0;
for(int i=0;i<=n;i++)
{
int t=-1;
for(int j=0;j<=n;j++)
{
if(!st[j]&&(t==-1||dist[j]<dist[t]))
{
t=j;
}
}
st[t]=true;
res+=dist[t];
for(int j=0;j<=n;j++)
{
dist[j]=min(dist[j],w[t][j]);
}
}
cout<<res<<endl;
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
int x;
cin>>x;
w[i][0]=w[0][i]=x;
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
cin>>w[i][j];
}
}
prim();
return 0;
}
#include
using namespace std;
const int N=510;
const int M=5210;
int n,m1,m2;
int h[N],e[M],ne[M],w[M],idx;
int dist[N];
int cnt[N];
bool st[N];
void add(int a,int b,int c)
{
e[idx]=b,ne[idx]=h[a],w[idx]=c,h[a]=idx++;
}
bool spfa()
{
memset(dist,0x3f,sizeof dist);
memset(st,false ,sizeof st);
memset(cnt,0,sizeof cnt);
queue<int>q;
for(int i=1;i<=n;i++)
{
q.push(i);
st[i]=true;
}
while(q.size())
{
auto t=q.front();
q.pop();
st[t]=false;
for(int i=h[t];~i;i=ne[i])
{
int j=e[i];
if(dist[j]>dist[t]+w[i])
{
dist[j]=dist[t]+w[i];
cnt[j]=cnt[t]+1;
if(cnt[j]>=n)return true;
if(!st[j])
{
st[j]=true;
q.push(j);
}
}
}
}
return false;
}
int main()
{
int _;
cin>>_;
while(_--)
{
cin>>n>>m1>>m2;
memset(h,-1,sizeof h);
idx=0;
for(int i=0;i<m1;i++)
{
int a,b,c;
cin>>a>>b>>c;
add(a,b,c),add(b,a,c);
}
for(int i=0;i<m2;i++)
{
int a,b,c;
cin>>a>>b>>c;
add(a,b,-c);
}
if(spfa())cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}
#include
using namespace std;
const int N=110;
const int M=N*N;
int h[N],e[M],ne[M],idx;
int ind[N];
int n;
void add(int a,int b)
{
e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
void topsort()
{
queue<int>q;
for(int i=1;i<=n;i++)
{
if(!ind[i])
{
q.push(i);
}
}
while(q.size())
{
auto t=q.front();
q.pop();
cout<<t<<' ';
for(int i=h[t];~i;i=ne[i])
{
int j=e[i];
if(--ind[j]==0)
{
q.push(j);
}
}
}
}
int main()
{
cin>>n;
memset(h,-1,sizeof h);
for(int i=1;i<=n;i++)
{
int son;
while(cin>>son,son)
{
add(i,son);
ind[son]++;
}
}
topsort();
return 0;
}
#include
#define x first
#define y second
using namespace std;
typedef pair<int,int>PII;
const int N=510;
const int M=N*N/2;
struct node
{
int a,b;
double w;
bool operator<(const node &t)const
{
return w<t.w;
}
}e[M];
int f[N];
int n,k;
PII p[N];
int find(int x)
{
if(x!=f[x])f[x]=find(f[x]);
return f[x];
}
double get_dist(PII a,PII b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int main()
{
cin>>n>>k;
for(int i=0;i<n;i++)cin>>p[i].x>>p[i].y;
int m=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<i;j++)
{
e[m++]={i,j,get_dist(p[i],p[j])};
}
}
sort(e,e+m);
for(int i=0;i<N;i++)f[i]=i;
int cnt=n;
double res=0;
for(int i=0;i<m;i++)
{
if(cnt<=k)break;
int a=e[i].a,b=e[i].b;
double w=e[i].w;
if(find(a)!=find(b))
{
f[find(b)]=find(a);
cnt--;
res=w;
}
}
printf("%.2f",res);
return 0;
}
#include
using namespace std;
const int N=6010;
struct node
{
int a,b,w;
bool operator<(const node &t)const
{
return w<t.w;
}
}e[N];
int f[N];
int cnt[N];
int n;
int find(int x)
{
if(x!=f[x])f[x]=find(f[x]);
return f[x];
}
int main()
{
int t;
cin>>t;
while(t--)
{
cin>>n;
for(int i=0;i<N;i++)f[i]=i,cnt[i]=1;
for(int i=0;i<n-1;i++)cin>>e[i].a>>e[i].b>>e[i].w;
sort(e,e+n-1);
int res=0;
for(int i=0;i<n-1;i++)
{
int a=find(e[i].a),b=find(e[i].b),w=e[i].w;
if(a!=b)
{
res+=(cnt[a]*cnt[b]-1)*(w+1);
f[b]=a;
cnt[a]+=cnt[b];
}
}
cout<<res<<endl;
}
return 0;
}
#include
using namespace std;
const int N=1010,M=5010;
const double eps=1e-4;
int n,m;
int w_ver[N];
int h[N],e[M],ne[M],w_edg[M],idx;
double dist[N];
int cnt[N];
bool st[N];
void add(int a,int b,int c)
{
e[idx]=b,ne[idx]=h[a],w_edg[idx]=c,h[a]=idx++;
}
bool check(double limit)
{
memset(dist,0,sizeof dist);
memset(cnt,0,sizeof cnt);
memset(st,0,sizeof st);
queue<int>q;
for(int i=1;i<=n;i++)
{
q.push(i);
st[i]=true;
}
while(q.size())
{
auto t=q.front();
q.pop();
st[t]=false;
for(int i=h[t];~i;i=ne[i])
{
int j=e[i];
if(dist[j]<dist[t]+w_ver[t]-w_edg[i]*limit)
{
dist[j]=dist[t]+w_ver[t]-w_edg[i]*limit;
cnt[j]=cnt[t]+1;
if(cnt[j]>=n)return true;
if(!st[j])
{
q.push(j);
st[j]=true;
}
}
}
}
return false;
}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)cin>>w_ver[i];
memset(h,-1,sizeof h);
while(m--)
{
int a,b,c;
cin>>a>>b>>c;
add(a,b,c);
}
double l=1,r=1000;
while(r-l>eps)
{
double mid=(l+r)/2;
if(check(mid))l=mid;
else r=mid;
}
printf("%.2f",r);
return 0;
}
#include
using namespace std;
const int N=40010,M=N*2;
int n,m;
int h[N],e[M],ne[M],idx;
int depth[N],fa[N][16];
void add(int a,int b)
{
e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
void bfs(int root)
{
memset(depth,0x3f,sizeof depth);
depth[0]=0,depth[root]=1;//哨兵,防止跳过根节点
queue<int>q;
q.push(root);
while(q.size())
{
auto t=q.front();
q.pop();
for(int i=h[t];~i;i=ne[i])
{
int j=e[i];
if(depth[j]>depth[t]+1)//j还没有被搜索过
{
depth[j]=depth[t]+1;
q.push(j);
fa[j][0]=t;//j向上跳2^0为t
for(int k=1;k<=15;k++)
fa[j][k]=fa[fa[j][k-1]][k-1];//2^j= 2^j-1 + 2^j-1
}
}
}
}
int lca(int a,int b)
{
if(depth[a]<depth[b])swap(a,b);
for(int k=15;k>=0;k--)
{
if(depth[fa[a][k]]>=depth[b])
{
a=fa[a][k];
}
}
if(a==b)return a;
for(int k=15;k>=0;k--)
{
if(fa[a][k]!=fa[b][k])
{
a=fa[a][k];
b=fa[b][k];
}
}
return fa[a][0];
}
int main()
{
cin>>n;
int root=0;
memset(h,-1,sizeof h);
for(int i=0;i<n;i++)
{
int a,b;
cin>>a>>b;
if(b==-1)root=a;
else add(a,b),add(b,a);
}
bfs(root);
cin>>m;
while(m--)
{
int a,b;
cin>>a>>b;
int p=lca(a,b);
if(p==a)puts("1");
else if(p==b)puts("2");
else puts("0");
}
return 0;
}
#include
using namespace std;
typedef pair<int,int>PII;
const int N=10010,M=N*2;
int n,m;
int h[N],e[M],ne[M],w[M],idx;
int dist[N];
int p[N];
int res[M];
int st[N];
vector<PII>query[N];
void add(int a,int b,int c)
{
e[idx]=b,ne[idx]=h[a],w[idx]=c,h[a]=idx++;
}
void dfs(int u,int fa)
{
for(int i=h[u];~i;i=ne[i])
{
int j=e[i];
if(j==fa)continue;
dist[j]=dist[u]+w[i];
dfs(j,u);
}
}
int find(int x)
{
if(p[x]!=x)p[x]=find(p[x]);
return p[x];
}
void tarjan(int u)
{
st[u]=1;
for(int i=h[u];~i;i=ne[i])
{
int j=e[i];
if(!st[j])
{
tarjan(j);
p[j]=u;
}
}
for(auto t:query[u])
{
int y=t.first,id=t.second;
if(st[y]==2)
{
int anc=find(y);
res[id]=dist[u]+dist[y]-dist[anc]*2;
}
}
st[u]=2;
}
int main()
{
cin>>n>>m;
memset(h,-1,sizeof h);
for(int i=0;i<n-1;i++)
{
int a,b,c;
cin>>a>>b>>c;
add(a,b,c);
add(b,a,c);
}
for(int i=0;i<m;i++)
{
int a,b;
cin>>a>>b;
if(a!=b)
{
query[a].push_back({b,i});
query[b].push_back({a,i});
}
}
for(int i=1;i<=n;i++)p[i]=i;
dfs(1,-1);
tarjan(1);
for(int i=0;i<m;i++)
cout<<res[i]<<endl;
return 0;
}
#include
using namespace std;
const int N=20010,M=200010;
int n,m;
int h[N],e[M],ne[M],w[M],idx;
int color[N];
void add(int a,int b,int c)
{
e[idx]=b,ne[idx]=h[a],w[idx]=c,h[a]=idx++;
}
bool dfs(int u,int c,int limit)
{
color[u]=c;
for(int i=h[u];~i;i=ne[i])
{
int j=e[i];
if(w[i]<=limit)continue;
if(color[j])
{
if(color[j]==c)return false;
}
else if(!dfs(j,3-c,limit))return false;
}
return true;
}
bool check(int limit)
{
memset(color,0,sizeof color);
for(int i=1;i<=n;i++)
{
if(color[i]==0)
{
if(!dfs(i,1,limit))return false;
}
}
return true;
}
int main()
{
cin>>n>>m;
memset(h,-1,sizeof h);
while(m--)
{
int a,b,c;
cin>>a>>b>>c;
add(a,b,c);
add(b,a,c);
}
int l=0,r=1e9;
while(l<r)
{
int mid=l+r>>1;
if(check(mid))r=mid;
else l=mid+1;
}
cout<<l<<endl;
return 0;
}
#include
using namespace std;
#define x first
#define y second
typedef pair<int,int>PII;
const int N=110;
int n,m;
PII match[N][N];
bool g[N][N],st[N][N];
int dx[4]={-1,0,1,0};
int dy[4]={0,1,0,-1};
bool find(int x,int y)
{
for(int i=0;i<4;i++)
{
int a=x+dx[i];
int b=y+dy[i];
if(a>0&&a<=n&&b>0&&b<=n&&!g[a][b]&&!st[a][b])
{
st[a][b]=true;
auto t=match[a][b];
if(t.x==-1||find(t.x,t.y))
{
match[a][b]={x,y};
return true;
}
}
}
return false;
}
int main()
{
cin>>n>>m;
while(m--)
{
int x,y;
cin>>x>>y;
g[x][y]=true;
}
memset(match,-1,sizeof match);
int res=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if((i+j)%2&&!g[i][j])
{
memset(st,0,sizeof st);
if(find(i,j))res++;
}
}
}
cout<<res<<endl;
return 0;
}
#include
using namespace std;
const int N=110;
int n,m,k;
int match[N];
bool g[N][N],st[N];
bool find(int x)
{
for(int i=0;i<m;i++)
{
if(!st[i]&&g[x][i])
{
st[i]=true;
if(match[i]==-1||find(match[i]))
{
match[i]=x;
return true;
}
}
}
return false;
}
int main()
{
while(cin>>n,n)
{
cin>>m>>k;
memset(g,0,sizeof g);
memset(match,-1,sizeof match);
while(k--)
{
int t,a,b;
cin>>t>>a>>b;
if(!a||!b)continue;
g[a][b]=true;
}
int res=0;
for(int i=0;i<n;i++)
{
memset(st,0,sizeof st);
if(find(i))res++;
}
cout<<res<<endl;
}
return 0;
}
#include
using namespace std;
int main()
{
int a, b;
cin >> a >> b;
int c = a + b;
string num = to_string(c);
string res;
for (int i = num.size() - 1,j = 0; i >= 0; i--)
{
res += num[i];
j++;
if (j % 3 == 0&&i&&num[i-1]!='-')res += ",";
}
reverse(res.begin(),res.end());
cout << res << endl;
return 0;
}
#include
using namespace std;
string num[15]={"zero","one","two","three","four","five","six","seven","eight","nine","ten"};
int main()
{
string s;
cin>>s;
long long res=0;
for(int i=0;i<s.size();i++)res+=s[i]-'0';
vector<string>v;
if(res==0)v.push_back(num[0]);
while(res)
{
v.push_back(num[res%10]);
res/=10;
}
for(int i=v.size()-1;i>=0;i--)cout<<v[i]<<' ';
return 0;
}
#include
using namespace std;
const int N=15;
struct node
{
string id;
string start;
string ed;
}e[N];
int n;
bool cmp1(node a,node b)
{
int h1=a.start[0]*10+a.start[1];
int m1=a.start[3]*10+a.start[4];
int s1=a.start[6]*10+a.start[7];
int h2=b.start[0]*10+b.start[1];
int m2=b.start[3]*10+b.start[4];
int s2=b.start[6]*10+b.start[7];
if(h1!=h2)return h1<h2;
else if(m1!=m2)return m1<m2;
else if(s1!=s2)return s1<s2;
return true;
}
bool cmp2(node a,node b)
{
int h1=a.ed[0]*10+a.ed[1];
int m1=a.ed[3]*10+a.ed[4];
int s1=a.ed[6]*10+a.ed[7];
int h2=b.ed[0]*10+b.ed[1];
int m2=b.ed[3]*10+b.ed[4];
int s2=b.ed[6]*10+b.ed[7];
if(h1!=h2)return h1>h2;
else if(m1!=m2)return m1>m2;
else if(s1!=s2)return s1>s2;
return true;
}
int main()
{
cin>>n;
for(int i=0;i<n;i++)
{
string a,b,c;
cin>>a>>b>>c;
e[i]={a,b,c};
}
sort(e,e+n,cmp1);
cout<<e[0].id<<' ';
sort(e,e+n,cmp2);
cout<<e[0].id;
return 0;
}
#include
using namespace std;
int main()
{
int n;
cin>>n;
int m=0;
int s=n;
vector<string>v;
while(s--)
{
string a,b;
cin>>a>>b;
bool f=false;
for(int i=0;i<b.size();i++)
{
if(b[i]=='1')
{
f=true;
b[i]='@';
}
if(b[i]=='0')
{
f=true;
b[i]='%';
}
if(b[i]=='l')
{
b[i]='L';
f=true;
}
if(b[i]=='O')
{
b[i]='o';
f=true;
}
}
if(f)
{
m++;
v.push_back(a+" "+b);
}
}
if(m==0)
{
if(n>1)
printf("There are %d accounts and no account is modified\n",n);
else puts("There is 1 account and no account is modified");
}
else
{
cout<<m<<endl;
for(int i=0;i<v.size();i++)cout<<v[i]<<endl;
}
return 0;
}
#include
using namespace std;
const int N=110;
int n;
struct node
{
string name;
char sex;
string id;
int sum;
}a[N],b[N];
bool cmp1(node a,node b)
{
return a.sum>b.sum;
}
bool cmp2(node a,node b)
{
return a.sum<b.sum;
}
int main()
{
cin>>n;
int j=0,k=0;
for(int i=0;i<n;i++)
{
string name,id;
char sex;
int sum;
cin>>name>>sex>>id>>sum;
if(sex=='F')a[j++]={name,sex,id,sum};
else b[k++]={name,sex,id,sum};
}
sort(a,a+j,cmp1),sort(b,b+k,cmp2);
bool f=false;
if(j!=0)cout<<a[0].name<<' '<<a[0].id<<endl;
else cout<<"Absent"<<endl,f=true;
if(k!=0)cout<<b[0].name<<' '<<b[0].id<<endl;
else cout<<"Absent"<<endl,f=true;
if(!f)cout<<a[0].sum-b[0].sum<<endl;
else cout<<"NA"<<endl;
return 0;
}
#include
using namespace std;
int main()
{
string a,b;
getline(cin,a);
getline(cin,b);
unordered_map<char,int>mp;
for(int i=0;i<b.size();i++)mp[b[i]]++;
for(int i=0;i<a.size();i++)
{
if(!mp.count(a[i]))cout<<a[i];
}
return 0;
}
#include
using namespace std;
#define x first
#define y second
bool check(char c)
{
if(c>='A'&&c<='Z')return true;
if(c>='a'&&c<='z')return true;
if(c>='0'&&c<='9')return true;
return false;
}
char to_lower(char c)
{
if(c>='A'&&c<='Z')return c-'A'+'a';
}
int main()
{
string s;
getline(cin,s);
unordered_map<string,int>mp;
for(int i=0,j=0;i<s.size();i++)
{
if(check(s[i]))
{
j=i;
while(check(s[j])&&j<s.size())j++;
string word=s.substr(i,j-i);
string w;
for(int k=0;k<word.size();k++)
w+=to_lower(word[k]);
mp[w]++;
i=j;
}
}
int cnt=-1;
string word;
for(auto x:mp)
{
if(x.y>cnt||x.y==cnt&&x.x<word)
{
word=x.x;
cnt=x.y;
}
}
cout<<word<<' '<<cnt<<endl;
return 0;
}
#include
using namespace std;
string week[10]={"MON","TUE","WED","THU","FRI","SAT","SUN"};
bool check(char c)
{
if(c>='A'&&c<='G')return true;
return false;
}
int cmp(char c)
{
if(c>='A'&&c<='N')return 1;
if(c>='0'&&c<='9')return 2;
return 0;
}
bool judge(char c)
{
if(c>='A'&&c<='Z')return true;
if(c>='a'&&c<='z')return true;
return false;
}
int main()
{
string a,b,c,d;
cin>>a>>b>>c>>d;
bool f=false;
for(int i=0;i<min((int)a.size(),(int)b.size());i++)
{
if(a[i]==b[i]&&check(a[i])&&!f)
{
f=true;
printf("%s ",week[a[i]-'A'].c_str());
}
else if(a[i]==b[i]&&cmp(a[i])&&f)
{
if(cmp(a[i])==1)printf("%02d:",10+a[i]-'A');
else if(cmp(a[i])==2)printf("%02d:",a[i]-'0');
break;
}
}
for(int i=0;i<min((int)c.size(),(int)d.size());i++)
{
if(c[i]==d[i]&&judge(c[i]))
{
printf("%02d",i);
break;
}
}
return 0;
}
#include
using namespace std;
map<int,double,greater<int>>mp;
int main()
{
for(int i=0;i<2;i++)
{
int k;
cin>>k;
for(int j=0;j<k;j++)
{
int x;
double y;
cin>>x>>y;
mp[x]+=y;
}
}
int res=0;
for(auto x:mp)if(x.second)res++;
printf("%d ",res);
for(auto x:mp)
{
if(x.second)
printf("%d %.1f ",x.first,x.second);
}
return 0;
}
#include
using namespace std;
map<int,double,greater<int>>a;
map<int,double,greater<int>>b;
map<int,double,greater<int>>c;
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++)
{
int x;
double y;
cin>>x>>y;
a[x]=y;
}
int m;
cin>>m;
for(int i=0;i<m;i++)
{
int x;
double y;
cin>>x>>y;
b[x]=y;
}
for(auto x:a)
{
for(auto y:b)
{
c[x.first+y.first]+=x.second*y.second;
}
}
int res=0;
for(auto x:c)if(x.second)res++;
printf("%d ",res);
for(auto x:c)
{
if(x.second)printf("%d %.1f ",x.first,x.second);
}
return 0;
}
#include
using namespace std;
vector<int>c;
unordered_map<int,int>ma;
unordered_map<int,int>mb;
bool add(vector<int>&a)
{
int t=0;
for(auto x:a)ma[x]++;
for(int i=0;i<a.size();i++)
{
t+=a[i]*2;
c.push_back(t%10);
t/=10;
}
if(t)c.push_back(t);
while(c.size()>1&&c.back()==0)c.pop_back();
reverse(c.begin(),c.end());
for(auto x:c)mb[x]++;
for(auto x:ma)
{
for(auto y:mb)
{
if(x.first==y.first&&x.second!=y.second)return false;
}
}
return true;
}
int main()
{
string s;
cin>>s;
vector<int>v;
for(int i=s.size()-1;i>=0;i--)v.push_back(s[i]-'0');
if(add(v))puts("Yes");
else puts("No");
for(auto x:c)cout<<x;
return 0;
}
#include
using namespace std;
string s;
int k;
vector<int>add(vector<int>&a,vector<int>&b)
{
int t=0;
vector<int>c;
for(int i=0;i<a.size();i++)
{
t+=a[i]+b[i];
c.push_back(t%10);
t/=10;
}
if(t)c.push_back(t);
while(c.size()>1&&c.back()==0)c.pop_back();
reverse(c.begin(),c.end());
return c;
}
bool check(vector<int>a)
{
vector<int>b;
for(int i=a.size()-1;i>=0;i--)b.push_back(a[i]);
return a==b;
}
int main()
{
cin>>s>>k;
vector<int>a;
for(int i=s.size()-1;i>=0;i--)a.push_back(s[i]-'0');
int cnt=0;
while(k--)
{
vector<int>b;
for(int i=a.size()-1;i>=0;i--)b.push_back(a[i]);
if(check(a))break;
a=add(a,b);
cnt++;
}
for(auto x:a)cout<<x;
cout<<endl;
cout<<cnt<<endl;
return 0;
}
#include
using namespace std;
int main()
{
int a,b,c,d,e,f;
scanf("%d.%d.%d %d.%d.%d",&a,&b,&c,&d,&e,&f);
a+=d,b+=e,c+=f;
b+=c/29;
c%=29;
a+=b/17;
b%=17;
printf("%d.%d.%d",a,b,c);
return 0;
}
#include
using namespace std;
bool check(vector<int>&a)
{
vector<int>b;
for(int i=a.size()-1;i>=0;i--)b.push_back(a[i]);
return a==b;
}
vector<int>add(vector<int>&a,vector<int>&b)
{
vector<int>c;
int t=0;
for(int i=0;i<a.size();i++)
{
t+=a[i]+b[i];
c.push_back(t%10);
t/=10;
}
if(t)c.push_back(t);
while(c.size()>1&&c.back()==0)c.pop_back();
reverse(c.begin(),c.end());
return c;
}
int main()
{
string s;
vector<int>a,b;
cin>>s;
for(int i=s.size()-1;i>=0;i--)a.push_back(s[i]-'0');
int m=10;
while(m--)
{
if(check(a))
{
for(auto x:a)cout<<x;
printf(" is a palindromic number.\n");
break;
}
b.clear();
for(int i=a.size()-1;i>=0;i--)b.push_back(a[i]);
for(int i=a.size()-1;i>=0;i--)cout<<a[i];
cout<<" + ";
for(int i=b.size()-1;i>=0;i--)cout<<b[i];
cout<<" = ";
a=add(a,b);
for(auto x:a)cout<<x;
cout<<endl;
reverse(a.begin(),a.end());
}
if(!check(a))puts("Not found in 10 iterations.");
return 0;
}
#include
using namespace std;
const int N=110;
const int M=N*2;
int h[N],e[M],ne[M],idx;
bool st[N];
int cnt[N];
int n,m;
int max_h;
void add(int a,int b)
{
e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
void dfs(int u,int depth)
{
if(h[u]==-1)
{
cnt[depth]++;
max_h=max(max_h,depth);
return;
}
for(int i=h[u];~i;i=ne[i])
{
int j=e[i];
if(st[j])continue;
st[j]=true;
dfs(j,depth+1);
}
}
int main()
{
cin>>n>>m;
memset(h,-1,sizeof h);
while(m--)
{
int id;
int k;
cin>>id>>k;
for(int i=0;i<k;i++)
{
int x;
cin>>x;
add(id,x);
}
}
dfs(1,0);
for(int i=0;i<=max_h;i++)cout<<cnt[i]<<' ';
return 0;
}
#include
using namespace std;
const int N=35;
int in[N],pos[N];
typedef struct node
{
int data;
node *left,*right;
}node,*tree;
int n;
tree create(int len,int *in,int *pos)
{
if(len<=0)return nullptr;
tree T=(tree)malloc(sizeof (struct node));
T->left=T->right=nullptr;
T->data=pos[len-1];
int i=0;
for(i=0;i<len;i++)
{
if(in[i]==T->data)
{
break;
}
}
T->left=create(i,in,pos);
T->right=create(len-i-1,in+i+1,pos+i);
return T;
}
void bfs(tree T)
{
queue<tree>q;
q.push(T);
while(q.size())
{
auto t=q.front();
q.pop();
cout<<t->data<<' ';
if(t->left)q.push(t->left);
if(t->right)q.push(t->right);
}
}
int main()
{
cin>>n;
for(int i=0;i<n;i++)cin>>pos[i];
for(int i=0;i<n;i++)cin>>in[i];
tree T=create(n,in,pos);
bfs(T);
return 0;
}
#include
using namespace std;
const int N=1e4+10;
const int M=N*2+10;
int h[N],e[M],ne[M],idx;
vector<int>v;
bool st[N];
int f[N];
int n;
int max_h;
void add(int a,int b)
{
e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
int find(int x)
{
if(x!=f[x])f[x]=find(f[x]);
return f[x];
}
int dfs(int u,int fa)
{
st[u]=true;
int depth=0;
for(int i=h[u];~i;i=ne[i])
{
int j=e[i];
if(j==fa)continue;
if(st[j])continue;
st[j]=true;
depth=max(depth,dfs(j,u)+1);
}
return depth;
}
int main()
{
cin>>n;
for(int i=0;i<N;i++)f[i]=i;
memset(h,-1,sizeof h);
for(int i=0;i<n-1;i++)
{
int a,b;
cin>>a>>b;
if(find(a)!=find(b))f[find(a)]=find(b);
add(a,b);
add(b,a);
}
int res=0;
for(int i=1;i<=n;i++)
{
if(f[i]==i)res++;
}
if(res>=2)printf("Error: %d components",res);
else
{
for(int i=1;i<=n;i++)
{
memset(st,false, sizeof st);
int depth=dfs(i,-1);
if(depth>max_h)
{
v.clear();
max_h=depth;
v.push_back(i);
}
else if(depth==max_h)v.push_back(i);
}
for(auto x:v)cout<<x<<endl;
}
return 0;
}
#include
using namespace std;
const int N=1010;
int n;
typedef struct node
{
int data;
node *left,*right;
}node,*tree;
vector<int>v1,v2,v3;
tree create1(tree T,int x)
{
if(!T)
{
T=(tree)malloc(sizeof (struct node));
T->left=T->right=nullptr;
T->data=x;
}
else
{
if(x<T->data)T->left=create1(T->left,x);
else T->right=create1(T->right,x);
}
return T;
}
tree create2(tree T,int x)
{
if(!T)
{
T=(tree)malloc(sizeof (struct node));
T->left=T->right=nullptr;
T->data=x;
}
else
{
if(x<T->data)T->right=create2(T->right,x);
else T->left=create2(T->left,x);
}
return T;
}
void pre1(tree T)
{
if(!T)return;
v2.push_back(T->data);
pre1(T->left);
pre1(T->right);
}
void pre2(tree T)
{
if(!T)return;
v3.push_back(T->data);
pre2(T->left);
pre2(T->right);
}
void pos(tree T)
{
if (!T)return;
pos(T->left);
pos(T->right);
cout << T->data << ' ';
}
int main()
{
cin>>n;
tree T1=nullptr,T2=nullptr;
for(int i=0;i<n;i++)
{
int x;
cin>>x;
v1.push_back(x);
T1=create1(T1,x);
T2=create2(T2,x);
}
pre1(T1);
pre2(T2);
bool f=false,s=false;
if(v1==v2)
{
f=true;
}
if(v1==v3)
{
s=true;
}
if(f&&!s)
{
puts("YES");
pos(T1);
}
else if(!f&&s)
{
puts("YES");
pos(T2);
}
else if(f&&s)
{
puts("YES");
pos(T1);
}
else puts("NO");
return 0;
}
#include
using namespace std;
const int N=1010;
int n;
int tr[N];
int level[N],cnt;
void dfs(int u)
{
if(u*2<=n)dfs(u*2);
level[u]=tr[cnt++];
if(u*2+1<=n)dfs(u*2+1);
}
int main()
{
cin>>n;
for(int i=0;i<n;i++)cin>>tr[i];
sort(tr,tr+n);
dfs(1);
for(int i=1;i<=cnt;i++)cout<<level[i]<<' ';
return 0;
}
#include
using namespace std;
const int N = 35;
int a[N], b[N];
int c[N];
int n;
string op;
int x;
int cnt1, cnt2;
int k = 0;
vector<int>v;
typedef struct node
{
int data;
node* left, * right;
}node, * tree;
tree create(int len, int* c, int* b)
{
if (len <= 0)return nullptr;
tree T = (tree)malloc(sizeof(struct node));
T->left = T->right = nullptr;
T->data = c[0];
int i = 0;
for (i = 0; i < len; i++)
if (T->data == b[i])
break;
T->left = create(i, c + 1, b);
T->right = create(len - i - 1, c + i + 1, b + i + 1);
return T;
}
void pos(tree T)
{
if (!T)return;
pos(T->left);
pos(T->right);
v.push_back(T->data);
}
int main()
{
cin >> n;
while (cin >> op)
{
if (op == "Push")
{
cin >> x;
a[cnt1++] = x;
c[k++] = x;
}
else
b[cnt2++] = a[--cnt1];
}
for (int i = 0; i < k; i++)cout << c[i] << ' ';
cout << endl;
for (int i = 0; i < cnt2; i++)cout << b[i] << ' ';
cout<<endl;
tree T = nullptr;
T = create(k, c, b);
pos(T);
for (int i = 0; i < v.size(); i++)
{
cout << v[i];
if (i < v.size() - 1)cout << ' ';
}
return 0;
}
#include
using namespace std;
const int N = 110;
int n;
struct node
{
int l, r;
int data;
}tr[N];
vector<int>v;
int cnt;
void dfs(int u)
{
//if (u == -1)return;
if (~tr[u].l)dfs(tr[u].l);
tr[u].data = v[cnt++];
if (~tr[u].r)dfs(tr[u].r);
}
void bfs(int u)
{
queue<int>q;
q.push(u);
while (q.size())
{
auto t = q.front();
q.pop();
cout << tr[t].data << ' ';
if (~tr[t].l)q.push(tr[t].l);
if (~tr[t].r)q.push(tr[t].r);
}
}
int main()
{
cin >> n;
for (int i = 0; i < n; i++)
cin >> tr[i].l >> tr[i].r;
v.resize(n);
for (auto& x : v)cin >> x;
sort(v.begin(), v.end());
dfs(0);
bfs(0);
return 0;
}
#include
using namespace std;
const int N=15;
bool st[N];
vector<int>leve,in;
int n;
struct node
{
int data;
int left,right;
}tr[N];
void bfs(int root)
{
queue<int>q;
q.push(root);
while(q.size())
{
auto t=q.front();
q.pop();
leve.push_back(tr[t].data);
if(tr[t].left!=-1)q.push(tr[t].left);
if(tr[t].right!=-1)q.push(tr[t].right);
}
for(auto x:leve)cout<<x<<' ';
cout<<endl;
}
void dfs(int root)
{
if(tr[root].left!=-1)dfs(tr[root].left);
in.push_back(tr[root].data);
if(tr[root].right!=-1)dfs(tr[root].right);
}
int main()
{
cin>>n;
for(int i=0;i<n;i++)
{
tr[i].data=i;
char x,y;
cin>>x>>y;
if(x=='-') tr[i].right=-1;
else
{
tr[i].right=x-'0';
st[tr[i].right]=true;
}
if(y=='-') tr[i].left=-1;
else
{
tr[i].left=y-'0';
st[tr[i].left]=true;
}
}
int root=0;
for(int i=0;i<n;i++)
{
if(!st[i])
{
root=i;
break;
}
}
bfs(root);
dfs(root);
for(auto x:in)cout<<x<<' ';
return 0;
}
#include
using namespace std;
const int N = 55;
struct node
{
int l, r;
int data;
}tr[N];
bool st[N];
int n;
int ud;
int maxv;
void check(int u, int k)
{
if(u==-1)return;
if(maxv<k)
{
maxv=k;
ud=u;
}
check(tr[u].l,k*2);
check(tr[u].r,k*2+1);
}
int main()
{
cin >> n;
for (int i = 0; i < n; i++)
{
string a, b;
cin >> a >> b;
if (a != "-")
{
int t = 0;
for (int i = 0; i < a.size(); i++)
t = t * 10 + (a[i] - '0');
tr[i].l = t;
st[tr[i].l] = true;
}
else tr[i].l = -1;
if (b != "-")
{
int t = 0;
for (int i = 0; i < b.size(); i++)
t = t * 10 + (b[i] - '0');
tr[i].r = t;
st[tr[i].r] = true;
}
else tr[i].r = -1;
tr[i].data = i;
}
int root = -1;
for (int i = 0; i < n; i++)
if (!st[i])
{
root = i;
break;
}
check(root, 1);
if (maxv == n)cout << "YES " << ud << endl;
else cout << "NO " << root << endl;
return 0;
}
#include
using namespace std;
const int N=1010;
typedef struct node
{
int data;
node *left,*right;
}node ,*tree;
int n;
int cnt[N];
int max_h;
tree create(tree T,int x)
{
if(!T)
{
T=(tree)malloc(sizeof (struct node));
T->left=T->right=nullptr;
T->data=x;
}
else
{
if(x<=T->data)
{
T->left=create(T->left,x);
}
else T->right=create(T->right,x);
}
return T;
}
void dfs(tree T,int depth)
{
if(!T)return;
if(depth>max_h) max_h=depth;
cnt[depth]++;
if(T->left)dfs(T->left,depth+1);
if(T->right)dfs(T->right,depth+1);
}
int main()
{
cin>>n;
tree T=nullptr;
for(int i=0;i<n;i++)
{
int x;
cin>>x;
T=create(T,x);
}
dfs(T,1);
cout<<cnt[max_h]<<" + "<<cnt[max_h-1]<<" = "<<cnt[max_h]+cnt[max_h-1]<<endl;
return 0;
}
#include
using namespace std;
const int N=35;
int n;
int in[N],pos[N];
vector<int>res;
typedef struct node
{
int data;
node *left,*right;
}node,*tree;
tree create(int len,int *in,int *pos)
{
if(len<=0)return nullptr;
tree T=(tree)malloc(sizeof (struct node));
T->left=T->right=nullptr;
T->data=pos[len-1];
int i=0;
for(i=0;i<len;i++)
{
if(T->data==in[i])break;
}
T->left=create(i,in,pos);
T->right=create(len-i-1,in+i+1,pos+i);
return T;
}
void bfs(tree T)
{
queue<tree>q;
q.push(T);
int f=1;
while(q.size())
{
vector<int>v;
int s=q.size();
for(int i=0;i<s;i++)
{
auto t=q.front();
q.pop();
v.push_back(t->data);
if(t->left)q.push(t->left);
if(t->right)q.push(t->right);
}
if(f!=-1)reverse(v.begin(),v.end());
for(auto x:v)res.push_back(x);
v.clear();
f*=-1;
}
}
int main()
{
cin>>n;
for(int i=0;i<n;i++)cin>>in[i];
for(int i=0;i<n;i++)cin>>pos[i];
tree T=create(n,in,pos);
bfs(T);
for(auto x:res)cout<<x<<' ';
return 0;
}
#include
using namespace std;
const int N=50010;
int pre[N],in[N];
int n;
typedef struct node
{
int data;
node *left,*right;
}node ,*tree;
vector<int>v;
tree create(int len,int *pre,int *in)
{
if(len<=0)return nullptr;
tree T=(tree)malloc(sizeof (struct node));
T->left=T->right=nullptr;
T->data=pre[0];
int i=0;
for(i=0;i<len;i++)
{
if(in[i]==T->data)break;
}
T->left=create(i,pre+1,in);
T->right=create(len-i-1,pre+i+1,in+i+1);
v.push_back(T->data);
if(v.size()==1)
{
cout<<v[0];
exit(0);
}
return T;
}
int main()
{
cin>>n;
for(int i=0;i<n;i++)cin>>pre[i];
for(int i=0;i<n;i++)cin>>in[i];
tree T=create(n,pre,in);
return 0;
}
#include
using namespace std;
int main()
{
string s;
cin>>s;
cout<<"Hello "<<s;
return 0;
}
#include
using namespace std;
int main()
{
int a,b;
cin>>a>>b;
double c=a*1.0/b;
printf("%d/%.1f=%d",a,c,b);
return 0;
}
#include
using namespace std;
int main()
{
int n;
cin>>n;
if(n>=90)
{
printf("gong xi ni kao le %d fen!",n);
}
else printf("kao le %d fen bie xie qi!",n);
}
#include
using namespace std;
int n;
int main()
{
cin>>n;
while(n--)
{
int x,y,z;
cin>>x>>y>>z;
if(x*x+y*y+z*z==3*x*y*z)puts("Yes");
else puts("No");
}
return 0;
}
#include
using namespace std;
int main()
{
string s;
getline(cin,s);
for(int i=0,j=0;i<s.size();i++)
{
bool f=false;
if(s[i]=='6')
{
j=i;
while(s[j]=='6')j++;
if(j-i>3&&j-i<=9)
{
f=true;
cout<<'9';
i=j-1;
}
else if(j-i>9)
{
f=true;
cout<<27;
i=j-1;
}
}
if(!f)cout<<s[i];
}
}
#include
using namespace std;
int t;
int n;
int turn(int n)
{
int sum=0;
while(n)sum+=n%10,n/=10;
return sum;
}
int main()
{
cin>>t;
while(t--)
{
cin>>n;
set<int>s;
for(int i=2;i<=9;i++)
{
int num=turn(n*i);
s.insert(num);
}
if(s.size()>1)puts("NO");
else
{
for(auto x:s)cout<<x<<endl;
}
}
#include
using namespace std;
int n;
int main()
{
cin>>n;
long long m=1;
int wei=1;
while(m<n)
{
m=m*10+1;
wei++;
}
while(1)
{
cout<<m/n;
m=m%n;
if(m==0)break;
m=m*10+1;
wei++;
}
cout<<' '<<wei;
}
#include
using namespace std;
const int N=1010;
int n;
int s[N];
int main()
{
cin>>n;
for(int i=0;i<n;i++)
{
int x,y,z;
scanf("%d-%d%d",&x,&y,&z);
s[x]+=z;
}
int maxv=-1;
int id=-1;
for(int i=0;i<N;i++)
{
if(maxv<s[i])
{
id=i;
maxv=s[i];
}
}
cout<<id<<' '<<maxv;
}
#include
using namespace std;
int n,m,k;
int main()
{
cin>>n>>m>>k;
while(k--)
{
stack<int>stk;
bool f=false;
int idx=1;
for(int i=1;i<=n;i++)
{
int x;
cin>>x;
if(x!=idx)
{
stk.push(x);
if(stk.size()>m)f=true;
}
else
{
idx++;
while(stk.size()&&stk.top()==idx)idx++,stk.pop();
}
}
if(!f&&idx>=n)puts("YES");
else puts("NO");
}
return 0;
}
#include
using namespace std;
int main()
{
int n, x;
cin >> n >> x;
int a[n];
for (auto &c : a) cin >> c;
sort(a, a + n);
long long res = 0;
for (int i = 0; i < n; i += 1) {
int aa = abs(x - a[i]);
int bb = x + a[i];
int l = i + 1;
int r = n - 1;
int L = -1;
int R = -1;
while (l <= r) {
int mid = (l + r) / 2;
if (a[mid] < bb && a[mid] > aa) {
L = mid;
r = mid - 1;
}
else if (a[mid] >= bb) {
r = mid - 1;
} else {
l = mid + 1;
}
}
l = i + 1;
r = n - 1;
while (l <= r) {
int mid = (l + r) / 2;
if (a[mid] < bb && a[mid] > aa) {
R = mid;
l = mid + 1;
} else if (a[mid] >= bb) {
r = mid - 1;
} else {
l = mid + 1;
}
}
if (R != -1 && L != -1) {
res += R - L + 1;
}
}
cout << res;
return 0;
}
#include
using namespace std;
const int N=1010;
int n;
typedef struct node
{
int data;
node *left,*right;
}node,*tree;
vector<int>v1,v2,v3;
tree create1(tree T,int x)
{
if(!T)
{
T=(tree)malloc(sizeof (struct node));
T->left=T->right=nullptr;
T->data=x;
}
else
{
if(x<T->data)T->left=create1(T->left,x);
else T->right=create1(T->right,x);
}
return T;
}
tree create2(tree T,int x)
{
if(!T)
{
T=(tree)malloc(sizeof (struct node));
T->left=T->right=nullptr;
T->data=x;
}
else
{
if(x<T->data)T->right=create2(T->right,x);
else T->left=create2(T->left,x);
}
return T;
}
void pre1(tree T)
{
if(!T)return;
v2.push_back(T->data);
pre1(T->left);
pre1(T->right);
}
void pre2(tree T)
{
if(!T)return;
v3.push_back(T->data);
pre2(T->left);
pre2(T->right);
}
vector<int>v5;
void pos(tree T)
{
if (!T)return;
pos(T->left);
pos(T->right);
v5.push_back(T->data);
}
int main()
{
cin>>n;
tree T1=nullptr,T2=nullptr;
for(int i=0;i<n;i++)
{
int x;
cin>>x;
v1.push_back(x);
T1=create1(T1,x);
T2=create2(T2,x);
}
pre1(T1);
pre2(T2);
bool f=false,s=false;
if(v1==v2)
{
f=true;
}
if(v1==v3)
{
s=true;
}
if(f&&!s)
{
puts("YES");
pos(T1);
bool f=false;
for(auto x:v5)
{
if(f)cout<<' ';
f=true;
cout<<x;
}
}
else if(!f&&s)
{
puts("YES");
pos(T2);
bool f=false;
for(auto x:v5)
{
if(f)cout<<' ';
f=true;
cout<<x;
}
}
else if(f&&s)
{
puts("YES");
pos(T1);
bool f=false;
for(auto x:v5)
{
if(f)cout<<' ';
f=true;
cout<<x;
}
}
else puts("NO");
return 0;
}
#include
using namespace std;
const int N=210;
int g[N][N];
int n,m,k;
int main()
{
cin>>n>>m;
memset(g,0x3f,sizeof g);
for(int i=0;i<=n;i++)g[i][i]=0;
while(m--)
{
int a,b,c;
cin>>a>>b>>c;
g[a][b]=g[b][a]=min(g[a][b],c);
}
cin>>k;
int res=0;
int id=-1;
int cost_min=0x3f3f3f3f;
for(int i=1;i<=k;i++)
{
int num;
bool f=false;
int sum=0;
cin>>num;
set<int>s;
vector<int>v;
int start;
cin>>start;
s.insert(start);
v.push_back(start);
int last=start;
if(g[0][start]!=0x3f3f3f3f)sum+=g[0][start];
else
{
f=true;
continue;
}
for(int j=1;j<num;j++)
{
int x;
cin>>x;
s.insert(x);
v.push_back(x);
if(g[last][x]!=0x3f3f3f3f)
{
sum+=g[last][x];
}
else
{
f=true;
continue;
}
last=x;
}
if(f)continue;
if(g[last][0]!=0x3f3f3f3f)
{
sum+=g[last][0];
}
else
{
f=true;
continue;
}
if(s.size()==n&&v.size()==n)
{
res++;
if(cost_min>sum)
{
cost_min=sum;
id=i;
}
}
}
cout<<res<<endl;
cout<<id<<' '<<cost_min<<endl;
return 0;
}
#include
using namespace std;
const int N=110;
int h[N],e[N],ne[N],idx;
int n,m,k;
int w[N];
vector<vector<int>>ans;
vector<int>path;
void add(int a,int b)
{
e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
void dfs(int u,int s,vector<int>&path)
{
if(h[u]==-1)
{
if(s==k)ans.push_back(path);
return;
}
for(int i=h[u];~i;i=ne[i])
{
int j=e[i];
path.push_back(w[j]);
dfs(j,s+w[j],path);
path.pop_back();
}
}
int main()
{
cin>>n>>m>>k;
memset(h,-1,sizeof h);
for(int i=0;i<n;i++)cin>>w[i];
for(int i=0;i<m;i++)
{
int k,id;
cin>>id>>k;
for(int j=0;j<k;j++)
{
int x;
cin>>x;
add(id,x);
}
}
path.push_back(w[0]);
dfs(0,w[0],path);
sort(ans.begin(),ans.end(),greater<vector<int>>());
for(auto x:ans)
{
for(int i=0;i<x.size();i++)cout<<x[i]<<' ';
cout<<endl;
}
return 0;
}
#include
using namespace std;
const int N=210;
int h[N],e[N],ne[N],idx;
int cnt[N];
int max_h;
int n,m;
void add(int a,int b)
{
e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
void dfs(int u,int depth)
{
max_h=max(depth,max_h);
cnt[depth]++;
for(int i=h[u];~i;i=ne[i])
{
int j=e[i];
dfs(j,depth+1);
}
}
int main()
{
cin>>n>>m;
memset(h,-1,sizeof h);
while(m--)
{
int id,k;
cin>>id>>k;
for(int i=0;i<k;i++)
{
int x;
cin>>x;
add(id,x);
}
}
dfs(1,1);
int res=0,level=0;
for(int i=1;i<=max_h;i++)
{
if(res<cnt[i])
{
res=cnt[i];
level=i;
}
}
cout<<res<<' '<<level<<endl;
return 0;
}
#include
using namespace std;
int n;
double p,r;
const int N=1e5+10;
int h[N],e[N],ne[N],idx;
double w[N];
double res;
int max_h;
void add(int a,int b)
{
e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
void dfs(int u,int depth)
{
max_h=max(max_h,depth);
if(h[u]==-1)
{
res+=p*w[u]*pow(1+r,depth);
return;
}
for(int i=h[u];~i;i=ne[i])
{
int j=e[i];
dfs(j,depth+1);
}
}
int main()
{
memset(h,-1,sizeof h);
cin>>n>>p>>r;
r/=100;
for(int i=0;i<n;i++)
{
int k;
cin>>k;
if(k)
{
for(int j=0;j<k;j++)
{
int id;
cin>>id;
add(i,id);
}
}
else
{
int x;
cin>>x;
w[i]=x;
}
}
dfs(0,0);
printf("%.1f", res);
return 0;
}
#include
using namespace std;
int n;
double p,r;
const int N=1e5+10;
int h[N],e[N],ne[N],idx;
int max_h;
double res;
unordered_map<double,int>mp;
void add(int a,int b)
{
e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
void dfs(int u,int depth)
{
max_h=max(depth,max_h);
if(h[u]==-1)
{
double w=p*pow(1+r,depth);
if(res<w)res=w,mp[res]++;
else if(res==w)mp[res]++;
return;
}
for(int i=h[u];~i;i=ne[i])
{
int j=e[i];
dfs(j,depth+1);
}
}
int main()
{
cin>>n>>p>>r;
memset(h,-1,sizeof h);
r/=100;
int root=0;
for(int i=0;i<n;i++)
{
int x;
cin>>x;
if(x==-1)root=i;
else add(x,i);
}
dfs(root,0);
printf("%.2f %d",res,mp[res]);
return 0;
}
#include
using namespace std;
const int N=1e5+10;
int h[N],e[N],ne[N],idx;
double q,r;
int n;
double res=1e9;
int max_h;
unordered_map<double,int>mp;
void add(int a,int b)
{
e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
void dfs(int u,int depth)
{
max_h=max(max_h,depth);
if(h[u]==-1)
{
double w=q*pow(1+r,depth);
if(res>w)res=w,mp[res]++;
else if(res==w)mp[res]++;
}
for(int i=h[u];~i;i=ne[i])
{
int j=e[i];
dfs(j,depth+1);
}
}
int main()
{
cin>>n>>q>>r;
r/=100;
memset(h,-1,sizeof h);
for(int i=0;i<n;i++)
{
int k;
cin>>k;
for(int j=0;j<k;j++)
{
int x;
cin>>x;
add(i,x);
}
}
dfs(0,0);
printf("%.4f %d",res,mp[res]);
return 0;
}
#include
using namespace std;
const int N=1010;
vector<int>v;
vector<int>path;
vector<vector<int>>ans;
int n;
bool is_great=true,is_min=true;
void dfs(int u)
{
if(u*2>n)
{
ans.push_back(path);
for(int i=1;i<path.size();i++)
{
if(path[i-1]>path[i])is_min=false;
if(path[i-1]<path[i])is_great=false;
}
return;
}
if(u*2+1<=n)
{
path.push_back(v[u*2+1]);
dfs(u*2+1);
path.pop_back();
}
if(u*2<=n)
{
path.push_back(v[u*2]);
dfs(u*2);
path.pop_back();
}
}
int main()
{
cin>>n;
v.resize(n+1);
for(int i=1;i<=n;i++)cin>>v[i];
path.push_back(v[1]);
dfs(1);
for(auto x:ans)
{
for(int i=0;i<x.size();i++)cout<<x[i]<<' ';
cout<<endl;
}
if(is_great&&!is_min)puts("Max Heap");
else if(is_min&&!is_great)puts("Min Heap");
else puts("Not Heap");
}
#include
using namespace std;
const int N=21;
struct node
{
string data;
int left,right;
}tr[N];
int n;
bool is_leaf[N];
bool st[N];
string dfs(int u)
{
string left,right;
if(tr[u].left!=-1)
{
left=dfs(tr[u].left);
if(!is_leaf[tr[u].left])
left="("+left+")";
}
if(tr[u].right!=-1)
{
right=dfs(tr[u].right);
if(!is_leaf[tr[u].right])
right="("+right+")";
}
return left+tr[u].data+right;
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>tr[i].data>>tr[i].left>>tr[i].right;
if(tr[i].left!=-1)st[tr[i].left]=true;
if(tr[i].right!=-1)st[tr[i].right]=true;
if(!st[tr[i].left]&&!st[tr[i].right])is_leaf[i]=true;
}
int root=-1;
for(int i=1;i<=n;i++)
{
if(!st[i])
{
root=i;
break;
}
}
cout<< dfs(root)<<endl;
return 0;
}
#include
using namespace std;
const int N=510;
int g[N][N];
int dist[N];
bool st[N];
int w[N];
int sum_w[N];
int cnt[N];
int pre[N];
int n,m,s,d;
void dijkstra()
{
memset(dist,0x3f,sizeof dist);
sum_w[s]=w[s];
dist[s]=0;
cnt[s]=1;
for(int i=0;i<n;i++)
{
int t=-1;
for(int j=0;j<n;j++)
if(!st[j]&&(t==-1||dist[j]<dist[t]))
t=j;
st[t]=true;
for(int j=0;j<n;j++)
{
if(dist[j]>dist[t]+g[t][j])
{
dist[j]=dist[t]+g[t][j];
cnt[j]=cnt[t];
sum_w[j]=sum_w[t]+w[j];
pre[j]=t;
}
else if(dist[j]==dist[t]+g[t][j])
{
cnt[j]+=cnt[t];
// if(sum_w[j]
{
sum_w[j]=max(sum_w[j],sum_w[t]+w[j]);
pre[j]=t;
}
}
}
}
cout<<cnt[d]<<' '<<sum_w[d]<<endl;
}
int main()
{
cin>>n>>m>>s>>d;
for(int i=0;i<n;i++)cin>>w[i];
memset(g,0x3f,sizeof g);
for(int i=0;i<n;i++)g[i][i]=0;
for(int i=0