总结
模拟赛
P10124 [USACO18OPEN] Family Tree B
题意
改定一棵树,求两头牛的关系
正解
将每头牛离散化成编号,在去即可
错因
心得
少用
复杂度
- 时间复杂度:
- 空间复杂度:
代码
#include <bits/stdc++.h>
using namespace std;
vector <int> g[1005];
string v[1005];
string up[1005] , down[1005];
string a , b , ans="NOT RELATED";
int in[1005] , S[1005];
int fa[1005];
int n , tot;
void dfs (int x, int cnt)
{
S[x]=cnt;
for (auto i:g[x])
{
dfs (i , cnt+1);
}
}
int main ( )
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> a >> b;
v[++tot]=a;
v[++tot]=b;
for (int i=1;i<=n;i++)
{
cin >> up[i] >> down[i];
v[++tot]=up[i];
v[++tot]=down[i];
}
sort (v+1 , v+tot+1);
tot=unique (v+1 , v+tot+1)-(v+1);
for (int i=1;i<=n;i++)
{
int aaa , bbb;
aaa=lower_bound (v+1 , v+tot+1 , up[i])-v;
bbb=lower_bound (v+1 , v+tot+1 , down[i])-v;
g[aaa].push_back (bbb);
fa[bbb]=aaa;
in[bbb]++;
}
for (int i=1;i<=tot;i++)
{
if (in[i]==0)
{
dfs (i , 1);
break;
}
}
int aa=lower_bound (v+1 , v+tot+1 , a)-v;
int bb=lower_bound (v+1 , v+tot+1 , b)-v;
if (S[aa]<S[bb])
{
swap (aa , bb);
swap (a , b);
}
if (in[aa]>0&&in[bb]>0&&fa[aa]==fa[bb])
{
cout << "SIBLINGS";
return 0;
}
int x=0 , y=0 , kkk=aa;
for (int i=1;i<=S[aa]-S[bb];i++)
{
kkk=fa[kkk];
x++;
}
aa=kkk;
while (aa!=bb&&in[aa]>0&&in[bb]>0)
{
x++ , y++;
aa=fa[aa] , bb=fa[bb];
}
if (aa==bb)
{
if (y==0)
{
if (x==1) ans="mother";
else if (x==2) ans="grand-mother";
else
{
ans="";
for (int i=1;i<=x-2;i++)
{
ans+="great-";
}
ans+="grand-mother";
}
}
else if (y==1)
{
if (x==1) ans="sister";
else if (x==2) ans="aunt";
else
{
ans="";
for (int i=1;i<=x-2;i++)
{
ans+="great-";
}
ans+="aunt";
}
}
else
{
cout << "COUSINS";
return 0;
}
}
if (ans=="NOT RELATED") cout << ans;
else cout << b << " is the " << ans << " of " << a;
return 0;
}P1694 [USACO19FEB] The Great Revegetation B
题意
连接的每两个草地颜色不能相同,求最后最小的字典序
正解
从前往后选择即可,看一下之前的是否出现过
错因
写了拓扑排序
心得
要按照字典序来
复杂度
- 时间复杂度:
- 空间复杂度:
代码
#include <bits/stdc++.h>
using namespace std;
vector <int> g[105];
bool vis[105][5];
int n , m , x , y;
int main ( )
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
while (m--)
{
cin >> x >> y;
g[min (x , y)].push_back (max (x , y));
}
for (int i=1;i<=n;i++)
{
int s;
for (int j=1;j<=4;j++)
{
if (vis[i][j]==0)
{
s=j;
break;
}
}
cout << s;
for (auto j:g[i])
{
vis[j][s]=1;
}
}
return 0;
}P6149 [USACO20FEB] Triangles S
题意
选出三个点坐标分别为,在时,求三角形面积和
正解
将和每个点行相同的和列相同的所乘起来就是那个点的贡献,加起来
错因
我打的暴力
心得
想到了一点,但立刻否定了,也没有任何依据
复杂度
- 时间复杂度:
- 空间复杂度:
代码
#include <bits/stdc++.h>
using namespace std;
struct node
{
int x , y , id;
};
const int MOD=1e9+7;
node v[100005];
long long a1[100005] , b1[100005];
long long a2[100005] , b2[100005];
long long sum=0;
int n;
bool cmp1 (node x , node y)
{
if (x.x!=y.x) return x.x<y.x;
return x.y<y.y;
}
bool cmp2 (node x , node y)
{
if (x.y!=y.y) return x.y<y.y;
return x.x<y.x;
}
int main ( )
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i=1;i<=n;i++)
{
cin >> v[i].x >> v[i].y;
v[i].id=i;
}
sort (v+1 , v+n+1 , cmp1);
for (int i=1;i<=n;)
{
int ans=i;
while (ans<n&&v[ans+1].x==v[ans].x) ans++;
for (int j=i+1;j<=ans;j++)
{
a1[v[j].id]=a1[v[j-1].id]+(j-i)*(v[j].y-v[j-1].y);
}
for (int j=ans-1;j>=i;j--)
{
b1[v[j].id]=b1[v[j+1].id]+(ans-j)*(v[j+1].y-v[j].y);
}
i=ans+1;
}
sort (v+1 , v+n+1 , cmp2);
for (int i=1;i<=n;)
{
int ans=i;
while (ans<n&&v[ans+1].y==v[ans].y) ans++;
for (int j=i+1;j<=ans;j++)
{
a2[v[j].id]=a2[v[j-1].id]+(j-i)*(v[j].x-v[j-1].x);
}
for (int j=ans-1;j>=i;j--)
{
b2[v[j].id]=b2[v[j+1].id]+(ans-j)*(v[j+1].x-v[j].x);
}
i=ans+1;
}
for (int i=1;i<=n;i++)
{
sum+=(a1[i]+b1[i])*(a2[i]+b2[i]);
// cout << a1[i] << " " << b1[i] << " " << a2[i]<< " " << b2[i] << "\n";
sum%=MOD;
}
cout << sum;
return 0;
}