为什么用cin.get();会全部WA?

P3367 【模板】并查集

JustinQLZ @ 2018-07-12 10:43:33

请问这样为什么会WA??(本地是学校的Windows系统)

#include <iostream>
using namespace std;

int father[10001];
int find(const int &x) {
    if (father[x] != x)
        father[x] = find(father[x]);
    return father[x];
}
inline void unionn(const int &r1, const int &r2) {
    father[r2] = r1;
}

int main() {
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; ++i)
        father[i] = i;
    for (int i = 1; i <= m; ++i) {
        cin.get();
        if (cin.get() == '1') {
            int x, y;
            cin >> x >> y;
            unionn(find(x), find(y));
        } else {
            int x, y;
            cin >> x >> y;
            cout << (find(x) == find(y) ? 'Y':'N') << endl;
    }
    return 0;
}

by 氷スイカ233 @ 2018-07-12 10:48:48

@JustinQLZ 你用get(str,cin);试试???


by JustinQLZ @ 2018-07-12 10:56:06

@Ice_watermelon233 可是我只是想读入z,不需要读入一整行啊。。(而且那样会更慢吧ORZ)


by 氷スイカ233 @ 2018-07-12 11:05:03

@JustinQLZ 那我就不懂了233


by andyli @ 2018-07-12 11:44:34

@JustinQLZ

for (int i = 1; i <= m; ++i) {
        cin.get();        <----这一行干什么用
        if (cin.get() == '1') {

by andyli @ 2018-07-12 11:45:54

@JustinQLZ 建议你输入数字1


by andyli @ 2018-07-12 11:46:47

@Ice_watermelon233

\text{你看看我代码:}
#include <iostream>
using namespace std;
const int maxn = 200050;
int p[maxn], cnt;

int find(int x)
{
    return p[x] == x ? x : (p[x] = find(p[x]));
}
void hb(int x, int y)
{
    int u = find(p[x]), v = find(p[y]);
    if (u != v)
    {
        cnt--;
        p[u] = v;
    }
}
int main()
{
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        p[i] = i;
    cnt = n;
    while (m--)
    {
        int a, b, c;
        cin >> a >> b >> c;
        if (a == 1)
            hb(b, c);
        else if (find(b) == find(c))
            cout << "Y" << endl;
        else cout << "N" << endl;
    }
    return 0;
}

by 氷スイカ233 @ 2018-07-12 11:48:15

@andyli Orz总算看懂了qwq


by andyli @ 2018-07-12 11:50:08

另外cnt不需要(我的代码中有)


by JustinQLZ @ 2018-07-20 11:37:35

@andyli 其实我用cin >> ch已经过了23333搜了一下,大概是因为Wnidows下换行是\r\n,Linux下换行是\n?


by andyli @ 2018-07-20 12:27:30

@JustinQLZ cin >> ch 会跳过空白(包括\r, \n, \t等)


|