总结
T1
错误代码
C++ 复制 #include <bits/stdc++.h>
using namespace std ;
vector<int> op[100005 ];
int vis[100005 ];
queue<int> po;
void dfs (int x )
{
cout<< x<< ' ' ;
if (op[x].size ()== 0 )
{
return ;
}
for (int i= 0 ;i< op[x].size ();++ i)
{
if (vis[op[x][i]])
{
continue ;
}
vis[op[x][i]]= 1 ;
dfs (op[x][i]);
}
}
int main ()
{
int n,m;
cin>> n>> m;
for (int i= 1 ;i<= m;++ i)
{
int x,y;
cin>> x>> y;
op[x].push_back (y);
}
for (int i= 1 ;i<= n;++ i)
{
sort (op[i].begin (),op[i].end ());
}
dfs (1 );
cout<< endl;
po.push (1 );
memset (vis,0 ,sizeof (vis));
vis[1 ]= 1 ;
while (po.size ())
{
int top= po.front ();
cout<< top<< ' ' ;
po.pop ();
for (int i= 0 ;i< op[top].size ();++ i)
{
if (vis[op[top][i]])
{
continue ;
}
vis[op[top][i]]= 1 ;
po.push (op[top][i]);
}
}
return 0 ;
}
错误原因
dfs和bfs起点忘记标记
AC代码
C++ 复制 #include <bits/stdc++.h>
using namespace std ;
vector<int> op[100005 ];
int vis[100005 ];
queue<int> po;
void dfs (int x )
{
vis[x]= 1 ;
cout<< x<< ' ' ;
for (int i= 0 ;i< op[x].size ();++ i)
{
if (vis[op[x][i]])
{
continue ;
}
vis[op[x][i]]= 1 ;
dfs (op[x][i]);
}
}
int main ()
{
int n,m;
cin>> n>> m;
for (int i= 1 ;i<= m;++ i)
{
int x,y;
cin>> x>> y;
op[x].push_back (y);
}
for (int i= 1 ;i<= n;++ i)
{
sort (op[i].begin (),op[i].end ());
}
dfs (1 );
cout<< endl;
po.push (1 );
memset (vis,0 ,sizeof (vis));
vis[1 ]= 1 ;
cout<< 1 << ' ' ;
while (po.size ())
{
int top= po.front ();
po.pop ();
for (int i= 0 ;i< op[top].size ();++ i)
{
if (vis[op[top][i]])
{
continue ;
}
vis[op[top][i]]= 1 ;
po.push (op[top][i]);
cout<< op[top][i]<< ' ' ;
}
}
return 0 ;
}
T2
错误代码
C++ 复制 #include <bits/stdc++.h>
#define int long long
using namespace std ;
int x[100005 ];
int y[100005 ];
int z[100005 ];
int fa[100005 ];
int find (int x )
{
if (fa[x]== x)
{
return x;
}
fa[x]= find (fa[x]);
return fa[x];
}
void unionn (int x ,int y )
{
x= find (x);
y= find (y);
if (x!= y)
{
fa[x]= y;
}
return ;
}
signed main ()
{
int t;
cin>> t;
while (t-- )
{
int n,h,r;
cin>> n>> h>> r;
for (int i= 1 ;i<= n;++ i)
{
cin>> x[i]>> y[i]>> z[i];
fa[i]= i;
}
fa[n+ 1 ]= n+ 1 ;
for (int i= 1 ;i<= n;++ i)
{
for (int j= 1 ;j< i;++ j)
{
if (r* 4 >= (x[i]- x[j])* (x[i]- x[j])+ (y[i]- y[j])* (y[i]- y[j])+ (z[i]- z[j])* (z[i]- z[j]))
{
unionn (i,j);
}
}
}
for (int i= 1 ;i<= n;++ i)
{
if (z[i]+ r>= h)
{
unionn (n+ 1 ,i);
}
}
for (int i= 1 ;i<= n;++ i)
{
if (z[i]- r<= 0 )
{
unionn (0 ,i);
}
}
if (find (0 )== find (n+ 1 ))
{
cout<< "Yes \n " ;
}
else
{
cout<< "No \n " ;
}
}
return 0 ;
}
错误原因
公式r * r * 4写成了r * 4。
AC代码
C++ 复制 #include <bits/stdc++.h>
#define int long long
using namespace std ;
int fa[100005 ];
int x[100005 ];
int y[100005 ];
int z[100005 ];
int find (int x )
{
if (fa[x]== x)
{
return x;
}
return fa[x]= find (fa[x]);
}
void unionn (int x ,int y )
{
x= find (x);
y= find (y);
if (x!= y)
{
fa[x]= y;
}
return ;
}
signed main ()
{
int t;
cin>> t;
while (t-- )
{
int n,h,r;
cin>> n>> h>> r;
for (int i= 1 ;i<= n;++ i)
{
cin>> x[i]>> y[i]>> z[i];
fa[i]= i;
}
fa[n+ 1 ]= n+ 1 ;
fa[n+ 2 ]= n+ 2 ;
for (int i= 1 ;i< n;++ i)
{
for (int j= i+ 1 ;j<= n;++ j)
{
if (r* r* 4 >= (x[i]- x[j])* (x[i]- x[j])+ (y[i]- y[j])* (y[i]- y[j])+ (z[i]- z[j])* (z[i]- z[j]))
{
unionn (i,j);
}
}
}
for (int i= 1 ;i<= n;++ i)
{
if (z[i]+ r>= h)
{
unionn (n+ 1 ,i);
}
if (z[i]<= r)
{
unionn (n+ 2 ,i);
}
}
if (find (n+ 1 )== find (n+ 2 ))
{
cout<< "Yes \n " ;
}
else
{
cout<< "No \n " ;
}
}
return 0 ;
}
T3
错误代码
C++ 复制 #include <bits/stdc++.h>
using namespace std ;
int a[100005 ];
string ans[100005 ];
string sum[100005 ];
int main ()
{
int n;
cin>> n;
for (int i= 1 ;i<= n;++ i)
{
cin>> a[i];
ans[i]= to_string (i);
sum[i]= to_string (i);
}
int cnt= 1 ;
while (1 )
{
for (int i= 1 ;i<= n;++ i)
{
ans[i]= sum[i];
}
bool b= 0 ;
for (int i= 1 ;i<= n;++ i)
{
if (ans[i].find (to_string (a[i]))!=- 1 )
{
b= 1 ;
break ;
}
}
if (b)
{
break ;
}
cnt++ ;
for (int i= 1 ;i<= n;++ i)
{
sum[a[i]]= sum[a[i]]+ " " + ans[i];
}
}
cout<< cnt;
return 0 ;
}
正确思路
首先,我们在开始输入时,点i告诉点a[i]知道的生日,如果出现了环,也就是i==find(i),代表i告诉了一圈,最后回到了a[i],则用cnt代表环中有多少个点,在向上找的时候++cnt,找完之后取最小值,如果不是环,则将i和a[i]连边,输出最小值即可。
AC代码
C++ 复制 #include <bits/stdc++.h>
using namespace std ;
int cnt= 0 ;
int fa[1000005 ];
int find (int x )
{
cnt++ ;
if (fa[x]== x)
{
return x;
}
return find (fa[x]);
}
int main ()
{
int n;
cin>> n;
for (int i= 1 ;i<= n;++ i)
{
fa[i]= i;
}
int minn= INT_MAX;
for (int i= 1 ;i<= n;++ i)
{
cnt= 0 ;
int x;
cin>> x;
if (i== find (x))
{
minn= min (minn,cnt);
}
else
{
fa[i]= x;
}
}
cout<< minn;
return 0 ;
}
T4
AC代码
C++ 复制 #include <bits/stdc++.h>
using namespace std ;
string s,r;
void dfs (string x ,string y )
{
if (! x.size ())
{
return ;
}
int tmp= x.find (y[0 ]);
dfs (x.substr (0 ,tmp),y.substr (1 ,tmp));
dfs (x.substr (tmp+ 1 ),y.substr (tmp+ 1 ));
cout<< y[0 ];
}
int main ()
{
cin>> s>> r;
dfs (s,r);
return 0 ;
}
T5
错误代码
C++ 复制 #include <bits/stdc++.h>
using namespace std ;
string s;
string f;
int cnt= 0 ;
void xx (string x )
{
int tmp= x.size ();
if (! tmp)
{
return ;
}
xx (x.substr (0 ,tmp/ 2 ));
xx (x.substr (tmp/ 2 ));
if (x.find ('0' )==- 1 )
{
s+= "I" ;
}
else if (x.find ('1' )==- 1 )
{
s+= "B" ;
}
else
{
s+= "F" ;
}
}
int main ()
{
int n;
cin>> n;
cin>> f;
xx (f);
cout<< s;
return 0 ;
}
正确思路
我们可以按照题目给出的方法,每次把字符串分成左右两边,然后判断当前字符串类型,最后输出即可
AC代码
C++ 复制 #include <bits/stdc++.h>
using namespace std ;
string s;
string f;
int cnt= 0 ;
void xx (string x )
{
int tmp= x.size ();
if (tmp> 1 )
{
xx (x.substr (0 ,tmp/ 2 ));
xx (x.substr (tmp/ 2 ));
if (x.find ('0' )==- 1 )
{
s+= "I" ;
}
else if (x.find ('1' )==- 1 )
{
s+= "B" ;
}
else
{
s+= "F" ;
}
return ;
}
if (x.find ('0' )==- 1 )
{
s+= "I" ;
}
else if (x.find ('1' )==- 1 )
{
s+= "B" ;
}
else
{
s+= "F" ;
}
}
int main ()
{
int n;
cin>> n;
cin>> f;
xx (f);
cout<< s;
return 0 ;
}
T6
错误代码
C++ 复制 #include <bits/stdc++.h>
using namespace std ;
int a[100005 ];
int main ()
{
int n;
cin>> n;
for (int i= 1 ;i<= n;++ i)
{
cin>> a[i];
}
int ans= 1 ;
int cnt= 0 ;
int sum= 0 ;
int mmm= 0 ;
int maxx= INT_MIN;
int id;
for (int i= 1 ;i<= n;++ i)
{
cnt++ ;
sum+= a[i];
if (cnt== ans)
{
if (sum> maxx)
{
id= mmm;
maxx= sum;
}
mmm++ ;
ans*= 2 ;
sum= 0 ;
}
}
cout<< id;
return 0 ;
}
错误原因
计算的层数要从1开始算起
AC代码
C++ 复制 #include <bits/stdc++.h>
#define int long long
using namespace std ;
int a[100005 ];
signed main ()
{
int n;
cin>> n;
for (int i= 1 ;i<= n;++ i)
{
cin>> a[i];
}
int ans= 1 ;
int cnt= 0 ;
int sum= 0 ;
int mmm= 1 ;
int x= 1 ;
int maxx= INT_MIN;
int id;
for (int i= 1 ;i<= n;++ i)
{
cnt++ ;
sum+= a[i];
if (cnt== ans)
{
if (sum> maxx)
{
id= mmm;
maxx= sum;
}
mmm++ ;
ans+= x* 2 ;
x*= 2 ;
sum= 0 ;
}
}
if (sum> maxx)
{
id= mmm;
maxx= sum;
}
cout<< id;
return 0 ;
}